Re: Multiple queries in PHP

From: Date: Sun, 25 Jan 2009 20:58:14 +0000
Subject: Re: Multiple queries in PHP
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-287117@lists.php.net to get a copy of this message
2009/1/25 Ashley Sheridan <ash@ashleysheridan.co.uk>: > I've run into a bit of a problem. I put together a query using mysql > variables in the form set @m:= 0; with the select that uses it directly > after. For any wondering, the select was using it as an incremental > value which can't be hard coded, as the value will depend on the > ordering of the results of the query itself. > > The problem seems to be that while phpMyAdmin would execute this double > query perfectly well, php using mysql_query() was having problems, as > apparently it can't actually run multiple queries. > > Now the full query looks something like this: > > > mysql_query("SELECT @m:=0;"); > > $query = "SELECT * FROM( > SELECT profiles.id, ROUND(AVG(rated.score)) AS > rating, > COUNT(rated.score) AS total, @m:=@m+1 AS rank FROM > rated LEFT JOIN > profiles ON (profiles.id = rated.profile_id) GROUP BY > rated.profile_id > ORDER BY rating DESC, total DESC) AS ranking WHERE ranking.id=$id"; > $result = mysql_query($query); > > > which seems to be working OK so far, but does anyone know of any > potential pitfalls I might face when doing something like this? It's a security feature to prevent SQL injection. AFAIK you'll have no problems so long as all calls to mysql_query happen on the same DB connection so you might want to start using the linkid parameter. -Stuart -- http://stut.net/

« previous php.general (#287117) next »