简体   繁体   中英

Mysqli multi query PHP

I want to use php mysqli function, to query this:

SET @rownum = 0, @rank = 1, @prev_val = NULL;
SELECT @rownum := @rownum + 1 AS row,
    @rank := IF(@prev_val!=score,@rownum,@rank) AS rank,
    userid,
    @prev_val := score AS score
FROM (
    SELECT userid, sum(amount) as score 
    from leads WHERE date(time) >= '2013-08-15'
    group by userid
) exodus_entries 
 ORDER BY rank asc LIMIT 0,100;

I tried using the mysqli_query(link, query); function, but no luck, any help?

There is no need to use multi-query, and you risk security problems whenever you use multi-query.

The values for you user variables @rownum, @rank, @prev_val will remain in effect for the second query, as long as you run both query in the same connection.

So just run mysqli_query("SET ..."); and then run mysqli_query("SELECT..."); separately. It's much simpler to do this, and avoids at least one security risk.

Regarding your error:

mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given 

Always check the return value of mysqli_query(). It will return false if there's an error. And of course you can't run mysqli_fetch_row(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM