Mysqli multiple query - set variable produces a boolean error / how to skip this?

Got the following simple query that works fine through phpmyadmin, but when I add it to my php website, the results are not returned and no error or warning messages occur. If I remove "SET @N = -1;" then it works great.

<?php 
$db_connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true);
mysql_select_db(DB_NAME, $db_connect);

$test_query = mysql_query("SET @N=-1;SELECT `id`, (@N:=@N+1) AS `mycount` FROM `mydb`;");

for ($i = 0; $i <= mysql_num_rows($test_query)-1; $i++) {
   echo mysql_result($db_directorymap, $i, 0) . " " . mysql_result($db_directorymap, $i, 1) . "<br />";
}
?>

UPDATE: I just switched to mysqli, but of course I still have a problem with the mysql and mysqli_multi_query statements. It seems that when executing the first part of the query, the returned results are empty, so a logical error is given. I guess I need to skip the first set of results, but I don't know how to do this?

+2
source share
2 answers

, mysql_query , , . :

  • ( , ):

    mysql_query( "SET @N=-1" );
    mysql_query( "SELECT `id`, (@N:=@N+1) AS `mycount` FROM `mydb`" );
    
  • mysqli multi_query ( PDO , ).

: PHP multi_query. , mysqli::next_result. - , :

mysqli_multi_query($link, $query);
mysqli_next_result($link);

if ($result = mysqli_store_result($link)) {
    while ($row = mysqli_fetch_row($result)) {
        printf("%s\n", $row[0]);
    }
    mysqli_free_result($result);
}
+2

mysql_query , , SET, SELECT.

0

All Articles