Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created March 18, 2010 18:45
Show Gist options
  • Save kamipo/336716 to your computer and use it in GitHub Desktop.
Save kamipo/336716 to your computer and use it in GitHub Desktop.
<?php
$connect_info = file_get_contents('connect_info.json');
$connect_info = json_decode($connect_info, true);
foreach (explode(";", $connect_info["dsn"]) as $line) {
list($key, $value) = explode("=", $line);
$connect_info[$key] = $value;
}
$host = $connect_info["host"];
$user = $connect_info["user"];
$passwd = $connect_info["passwd"];
$dbname = $connect_info["dbname"];
$port = $connect_info["port"];
$user_id = $argv[1] ? $argv[1] : 0;
$dbh = mysql_connect("$host:$port", $user, $passwd);
if (!$dbh) {
printf("Connect failed: %s\n", mysql_error());
exit();
}
if (!mysql_select_db($dbname)) {
printf("Unable to select mydbname: %s\n", mysql_error());
exit();
}
$user_id = mysql_real_escape_string($user_id);
if ($result = mysql_query("SELECT * FROM pix_user WHERE user_id = '$user_id'")) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}
<?php
$connect_info = file_get_contents('connect_info.json');
$connect_info = json_decode($connect_info, true);
foreach (explode(";", $connect_info["dsn"]) as $line) {
list($key, $value) = explode("=", $line);
$connect_info[$key] = $value;
}
$host = $connect_info["host"];
$user = $connect_info["user"];
$passwd = $connect_info["passwd"];
$dbname = $connect_info["dbname"];
$port = $connect_info["port"];
$user_id = $argv[1] ? $argv[1] : 0;
$dbh = mysqli_connect($host, $user, $passwd, $dbname, $port);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$user_id = mysqli_real_escape_string($dbh, $user_id);
if ($result = mysqli_query($dbh, "SELECT * FROM pix_user WHERE user_id = '$user_id'")) {
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
}
<?php
$connect_info = file_get_contents('connect_info.json');
$connect_info = json_decode($connect_info, true);
foreach (explode(";", $connect_info["dsn"]) as $line) {
list($key, $value) = explode("=", $line);
$connect_info[$key] = $value;
}
$host = $connect_info["host"];
$user = $connect_info["user"];
$passwd = $connect_info["passwd"];
$dbname = $connect_info["dbname"];
$port = $connect_info["port"];
$user_id = $argv[1] ? $argv[1] : 0;
$dbh = new mysqli($host, $user, $passwd, $dbname, $port);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$user_id = $dbh->real_escape_string($user_id);
if ($result = $dbh->query("SELECT * FROM pix_user WHERE user_id = '$user_id'")) {
while ($row = $result->fetch_assoc()) {
print_r($row);
}
}
<?php
$connect_info = file_get_contents('connect_info.json');
$connect_info = json_decode($connect_info, true);
foreach (explode(";", $connect_info["dsn"]) as $line) {
list($key, $value) = explode("=", $line);
$connect_info[$key] = $value;
}
$host = $connect_info["host"];
$user = $connect_info["user"];
$passwd = $connect_info["passwd"];
$dbname = $connect_info["dbname"];
$port = $connect_info["port"];
$user_id = $argv[1] ? $argv[1] : 0;
$dbh = new mysqli($host, $user, $passwd, $dbname, $port);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($sth = $dbh->prepare("SELECT * FROM pix_user WHERE user_id = ?")) {
$sth->bind_param('i', $user_id);
$sth->execute();
$result = $sth->result_metadata();
while ($field = $result->fetch_field()) {
$row[$field->name] = &$row[$field->name];
}
call_user_func_array(array($sth, "bind_result"), $row);
while ($sth->fetch()) {
print_r($row);
}
}
<?php
$connect_info = file_get_contents('connect_info.json');
$connect_info = json_decode($connect_info);
$dsn = "mysql:" . $connect_info->dsn;
$user = $connect_info->user;
$passwd = $connect_info->passwd;
$user_id = $argv[1] ? $argv[1] : 0;
try {
$pdo = new PDO($dsn, $user, $passwd);
// see http://d.hatena.ne.jp/do_aki/20100221/1266746673
// $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, false);
$sth = $pdo->prepare("SELECT * FROM pix_user WHERE user_id = :ID");
$sth->bindValue(":ID", $user_id);
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
print_r($row);
}
} catch (PDOException $e) {
echo $e->getMessage();
}
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use DBI;
use File::Slurp;
use Try::Tiny;
use Data::Dumper;
sub print_r { print Dumper @_ }
my $connect_info = read_file('connect_info.json');
$connect_info = from_json($connect_info);
$connect_info->{dsn} = "DBI:mysql:" . $connect_info->{dsn};
my $user_id = $ARGV[0] || 0;
try {
my $dbh = DBI->connect(@{$connect_info}{qw/dsn user passwd/});
my $sth = $dbh->prepare("SELECT * FROM pix_user WHERE user_id = ?");
$sth->execute($user_id);
while (my $row = $sth->fetchrow_hashref) {
print_r($row);
}
} catch {
print $_;
};
#!/usr/bin/ruby
require "rubygems"
require "json"
require "dbi"
f = open("connect_info.json")
connect_info = f.read
f.close
connect_info = JSON.parse(connect_info)
dsn = "DBI:Mysql:" + connect_info["dsn"]
user = connect_info["user"]
passwd = connect_info["passwd"]
user_id = ARGV[0] || 0
begin
dbh = DBI.connect(dsn, user, passwd)
sth = dbh.prepare("SELECT * FROM pix_user WHERE user_id = ?")
sth.execute(user_id)
sth.fetch_hash do |row|
puts row
end
rescue DBI::DatabaseError => e
puts e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment