MySQL syntax error, but only when used via PHP

I am trying to reuse user IDs in my database, but I am 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 hash. \n";}
    else{echo "Changed $username hash<i>!</i> \n";}
}
mysql_close($conn);
?>

The whole request works through PHPMyAdmin exactly, it simply returns zero lines. But when I try it with actual values ​​passing through it, using it with PHP, it gets to 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 mistakes - I am not able to think about what it is. Thanks in advance,

Sam

+5
source share
3 answers

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

+7
source

From the documentation :

mysql_query() sends a unique query (multiple queries are not supported)

php. , php.

, . . PDO .

+2

, , (-):

try {
    // First of all, let begin a transaction
    $db->beginTransaction();

    // A set of queries ; of one fails, an exception should be thrown
    $db->query('first query');
    $db->query('second query');
    $db->query('third query');

    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed ; and we can commit the transaction
    $db->commit();
} catch (Exception $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $db->rollback();
}

, , , Exception:

PDO , ,   . PDO:: setAttribute    PDO:: ATTR_ERRMODE PDO:: ERRMODE_EXCEPTION   , - API, , , , , .

, : - : , .

For example, quite often you will have several requests before a transaction (before the start) and another pair of requests after a transaction (after a commit or rollback); and you want these requests to be executed regardless of what happened (or not) in the transaction.

==================================================== ===

You can also use mysqli to achieve the same

For ex

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* disable autocommit */
mysqli_autocommit($link, FALSE);

mysqli_query($link, "CREATE TABLE myCity LIKE City");
mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* commit insert */
mysqli_commit($link);
+1
source

All Articles