Got the following simple query which works fine through phpmyadmin but when I add it to my php website no results are returned and no error/warning messages either. If I remove
Multiple queries via mysql_query aren't supported, so I'd guess that it's only executing the SET command, and not the subsequent SELECT command.
It's because the mysql_query
function will only accept one query, but you've given it two, separated by a semicolon. Try either:
Running each query separately (don't know if this will work):
mysql_query( "SET @N=-1" );
mysql_query( "SELECT `id`, (@N:=@N+1) AS `mycount` FROM `mydb`" );
Using mysqli with the multi_query function (or a PDO equivalent if there is one).
To answer your updated question: check the PHP manual page for multi_query. I think you'll want to use mysqli::next_result
. Something like this, using procedural style:
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);
}