Skip to content

Instantly share code, notes, and snippets.

@avuserow
Created May 15, 2014 18:10
Show Gist options
  • Save avuserow/1c20cc03c007eda43c82 to your computer and use it in GitHub Desktop.
Save avuserow/1c20cc03c007eda43c82 to your computer and use it in GitHub Desktop.
MySQL oddities
1: got user $VAR1 = {
'uid' => '1'
};
with undef uid!
2: ok (didn't find a user)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
use DBI;
my %conf = (
dsn => 'dbi:mysql:database=my_test;host=localhost',
dbuser => 'me',
dbpass => undef,
);
my $dbh = DBI->connect($conf{'dsn'}, $conf{'dbuser'}, $conf{'dbpass'}, {RaiseError => 1, AutoCommit => 1});
$dbh->do('DROP TABLE IF EXISTS test_users');
$dbh->do('CREATE TABLE test_users (uid INTEGER PRIMARY KEY AUTO_INCREMENT, name varchar(20) NOT NULL);');
{
my $sth = $dbh->prepare('INSERT INTO test_users(name) values(?)');
$sth->execute('asdf' . time);
}
# If I uncomment this line, the problem disappears
#$dbh = DBI->connect($conf{'dsn'}, $conf{'dbuser'}, $conf{'dbpass'}, {RaiseError => 1, AutoCommit => 1});
for (1 .. 2) {
my $user;
# Load the user
my $sth = $dbh->prepare('SELECT uid FROM test_users WHERE uid IS NULL');
$sth->execute;
while (my $row = $sth->fetchrow_hashref()) {
$user = Dumper($row);
}
if ($user) {
print "$_: got user $user with undef uid!\n\n";
} else {
print "$_: ok (didn't find a user)\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment