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

后端 未结 2 1161
滥情空心
滥情空心 2021-01-22 08:17

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

相关标签:
2条回答
  • 2021-01-22 08:39

    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.

    0 讨论(0)
  • 2021-01-22 08:43

    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);
    }
    
    0 讨论(0)
提交回复
热议问题