Skip to content
Advertisement

PHP & MySQL: How can I use “SET @rank=0;” in $query=

In my PHP file, I use this line to pull data from my mySQL database:

JavaScript

If I check the SELECT statement in phpMyAdmin’s SQL window (without $query= ) it works fine.

But, if I use it in PHP, then I get an error. It doesn’t like the “SET @rank=0;” bit. Is there a way to use “SET @rank=0;” when it’s in “$query=” ? Is there a workaround?

The rest of the code is standard stuff for pulling data from a db:

JavaScript

Update: I’m trying to use Amerb’s suggestion of using multi-query. I concatenated the query like so:

JavaScript

I changed the result to:

JavaScript

But, it’s failing for some reason. I’m on a machine running PHP 5.2. Any suggestions?

Advertisement

Answer

This guy here seems to have a way of setting the variable in the same query to zero. I don’t have MySQL set on up on this machine to try it, though.

Here’s the query he suggests in his blog post:

JavaScript

(Is there some homework assignment coming due somewhere having to do with computing ranks? This is the third question I’ve seen on this in two days.)

Are you checking for duplicate scores?

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement