mysqli multiple queries - set variable produces boolean error/how to skip this?

佐手、 提交于 2019-12-01 23:37:27

It's because the mysql_query function will only accept one query, but you've given it two, separated by a semicolon. Try either:

  1. 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`" );
    
  2. 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);
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!