Nick
2006.10.03, 10:15 AM
I'm trying to do two consecutive MySQL queries in PHP, but the second one fails for no apparent reason.
Here are the functions I'm using to manage a connection:
function connectToDatabase() {
$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_selectdb(DB_NAME, $connection);
return $connection;
}
function closeConnection($connection) {
mysql_close($connection);
}
But when I use it like this:
$connection = connectToDatabase();
$query = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('$name', '$password', '$email')";
mysql_query($query, $connection) or die(mysql_error());
$theid = getUserID($name);
$query = "INSERT INTO `user_profiles` VALUES ('$theid', '0', '0')";
mysql_query($query, $connection) or die(mysql_error());
closeConnection($connection);
There's no output, but the query never succeeds and the whole script stops right there. I had it print out $query once and ran it manually (in phpMyAdmin) and it worked just as expected.
But if I close the connection and reopen it in between the queries, it works just fine. Any ideas?
Here are the functions I'm using to manage a connection:
function connectToDatabase() {
$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_selectdb(DB_NAME, $connection);
return $connection;
}
function closeConnection($connection) {
mysql_close($connection);
}
But when I use it like this:
$connection = connectToDatabase();
$query = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('$name', '$password', '$email')";
mysql_query($query, $connection) or die(mysql_error());
$theid = getUserID($name);
$query = "INSERT INTO `user_profiles` VALUES ('$theid', '0', '0')";
mysql_query($query, $connection) or die(mysql_error());
closeConnection($connection);
There's no output, but the query never succeeds and the whole script stops right there. I had it print out $query once and ran it manually (in phpMyAdmin) and it worked just as expected.
But if I close the connection and reopen it in between the queries, it works just fine. Any ideas?