Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

select

mysql

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

$query = "SET @rank=0; SELECT @rank:=@rank +1 as rank, Blah Blah...";

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:

public function getmyData() {


 $mysql = mysql_connect(connection stuff);

 $query = "SELECT @rank:=@rank +1 as rank, formatted_school_name,  blah blah";

 $result = mysql_query($query);

            $ret = array();
                 while ($row = mysql_fetch_object($result)) {
                    $tmp = new VOmyData1();
                    $tmp->stuff1 = $row-> stuff1;
                    $tmp->stuff2 = $row->stuff2;

                    $ret[] = $tmp; 
                        }
                 mysql_free_result($result);

                 return $ret;

    }   

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

$query = "SET @rank = 0";

$query .= "SELECT @rank:=@rank +1 as rank...

I changed the result to:

$result = $mysqli_multi_query($query);

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

like image 466
Laxmidi Avatar asked Aug 17 '11 23:08

Laxmidi


People also ask

What PHP stand for?

PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym." PHP executes on the server, while a comparable alternative, JavaScript, executes on the client.

What is PHP or HTML?

PHP is a server-side programming language. HTML is a client-side scripting language. PHP is used in backend development, which interacts with databases to retrieve, store, and modify the information. HTML is used in frontend development, which organizes the content of the website.

Which is better Python or PHP?

Python is better than PHP in long term project. PHP has low learning curve, it is easy to get started with PHP. Compare to PHP Python has lower number of Frameworks. Popular ones are DJango, Flask.

Is PHP used anymore?

PHP is known to be the most frequently used programming language. According to W3Techs, 78.8% of all websites are using PHP for their server-side. Interesting fact: PHP originally stood for Personal Home Page. Now PHP is widely known and thought of as Hypertext Preprocessor.


2 Answers

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:

select @rownum:=@rownum+1 ‘rank’, p.* from player p, (SELECT @rownum:=0) r order by score desc limit 10;

(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?

like image 179
Marvo Avatar answered Sep 24 '22 18:09

Marvo


You have to enable the use of multiple queries in one, but i forgot how do do this at the moment. It's a security feature.

like image 36
Johni Avatar answered Sep 21 '22 18:09

Johni