Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL syntax error, but only when using through PHP

Tags:

syntax

php

mysql

I'm trying to re-hash user IDs in my database, but I'm getting stuck.

<?php
include("session.php");
include("functions.php");
$conn = ConnectMySQL();
setTimezone($session->username);

$sql = "SELECT username, userid FROM users";
$result = mysql_query($sql) or die(mysql_error());
while($rows = mysql_fetch_array($result)){
    $username = $rows['username'];
    $old = mysql_real_escape_string($rows['userid']);
    $new = mysql_real_escape_string($session->generateRandID());

    $moresql = "START TRANSACTION;
                UPDATE users SET userid='$new' WHERE userid='$old';
                UPDATE comments SET user='$new' WHERE user='$old';
                UPDATE forum_posts SET poster_name='$new' WHERE poster_name='$old';
                UPDATE forum_topics SET topic_poster_name='$new' WHERE topic_poster_name='$old';
                UPDATE images SET author='$new' WHERE author='$old';
                UPDATE likes SET user='$new' WHERE user='$old';
                UPDATE music SET author='$new' WHERE author='$old';
                UPDATE ratings SET user='$new' WHERE user='$old';
                COMMIT;";
    $newresult = mysql_query($moresql) or die(mysql_error());
    if(!$newresult){echo "There was a problem with changing $username's hash. \n";}
    else{echo "Changed $username's hash<i>!</i> \n";}
}
mysql_close($conn);
?>

The whole query works through PHPMyAdmin absolutely fine, just returns zero rows obviously. But when I try it with actual values being passed through it using it with PHP it gets suck:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE users SET userid='b8aca4b680707453fa4dfe1bf1d0fddb' WHERE userid='8' at line 2

There are no other errors - I'm at a loss to think of what it is. Thanks in advance,

Sam

like image 692
2xAA Avatar asked Sep 10 '12 16:09

2xAA


1 Answers

Multiple queries are not supported by mysql_query(). You must execute one query at a time. See http://php.net/manual/en/function.mysql-query.php

like image 123
rationalboss Avatar answered Sep 22 '22 12:09

rationalboss