简体   繁体   中英

Different result posting sql query through php than through phpmyadmin

Sorry if it's been asked before but I think this might be very specific to the query..I'm very new to both PHP and SQL but got a high scores system working and now stuck on one last thing...I have a query that if I paste into phpmyadmin it works fine, but when included between $sql ="" (in PHP) it doesn't. In a vain attempt to debug it calling via a browser I can see the offending item just can't see why, in PHP:

$sql = "SET @rownum := 0; SELECT * FROM (SELECT @rownum := @rownum+1 AS rank, ID, Username, Score, UDID FROM users ORDER BY Score DESC)
AS derived_table WHERE Username = 'Dave';";

This returns nothing...but paste it into phpmyadmin and it works fine, returns two entries sorted by score and displaying their rank.

If I remove SET @rownum := 0; then it works but returns null for rank...so I guess the ; is causing it to terminate?

Any ideas would be a big help before I pull the last piece of hair out.

In a regular mysql API in PHP, this is not possible. Try executing your queries separately (SET & SELECT) and use mysqli_multi_query . http://se2.php.net/manual/en/mysqli.multi-query.php

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