chiprivers Posted November 19, 2007 Share Posted November 19, 2007 I have a simple script that loops through the contents of an array querying a database on each itteration for the current value of the array element. Within the loop there is a simple: <?php $query = "blah blah blah"; $reault = mysql_query($query); while ($row = mysql_fetch_array($result)) { // do this stuff } ?> This is working fine on the first itteration but on the second the script fails and gives the following error: Warning: mysql_fetch_array() supplied argument is not a valid MYSQL result source in blah blah... Any idea why this is working on the first itteration and then failing? Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted November 19, 2007 Share Posted November 19, 2007 Did you copy this code? because you have a spelling error. <?php $query = "blah blah blah"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { // do this stuff } ?> The $result was spelled $reault Quote Link to comment Share on other sites More sharing options...
chiprivers Posted November 19, 2007 Author Share Posted November 19, 2007 Did you copy this code? because you have a spelling error. <?php $query = "blah blah blah"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { // do this stuff } ?> The $result was spelled $reault no, unfortunatley I have just done a typo on the post! Quote Link to comment Share on other sites More sharing options...
rlindauer Posted November 19, 2007 Share Posted November 19, 2007 Can you post the entire loop? Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 19, 2007 Share Posted November 19, 2007 Try to run the query directly on mysql. Quote Link to comment Share on other sites More sharing options...
chiprivers Posted November 19, 2007 Author Share Posted November 19, 2007 Can you post the entire loop? <?php $pda[wr] = 1; $pda[alp] = 1; $stns = array(21,22,23,24,25); foreach ($pda as $type => $qty) { $first = 1; foreach ($stns as $key => $stn) { if ($first == 0) { $query .= " UNION "; } else { $query = 0; } $query = "SELECT a.*, s.*, o.* FROM appliances AS a LEFT JOIN appliance_status AS s ON (a.status = s.abbr) LEFT JOIN overlays AS o ON (a.loc = o.stn) WHERE a.loc = ".$stn." AND a.inc_av = 1 AND a.type LIKE '%".$type."%'"; $result = mysql_query($query); // display results } } ?> I have had to type this rather than copy and past as I am working on a different machine. I have details all the relevent bits and hopefully no typos! Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 19, 2007 Share Posted November 19, 2007 Replace the variables with a known value then try to run this directly on mysql SELECT a.*, s.*, o.* FROM appliances a LEFT JOIN appliance_status s ON (a.status = s.abbr) LEFT JOIN overlays o ON (a.loc = o.stn) WHERE a.loc = ".$stn." AND a.inc_av = 1 AND a.type LIKE '%".$type."%'; versus SELECT a.*, s.*, o.* FROM appliances AS a LEFT JOIN appliance_status AS s ON (a.status = s.abbr) LEFT JOIN overlays AS o ON (a.loc = o.stn) WHERE a.loc = ".$stn." AND a.inc_av = 1 AND a.type LIKE '%".$type."%'; Quote Link to comment Share on other sites More sharing options...
chiprivers Posted November 19, 2007 Author Share Posted November 19, 2007 No probs in mysql! Quote Link to comment Share on other sites More sharing options...
chiprivers Posted November 19, 2007 Author Share Posted November 19, 2007 Eureka!! Found the problem! I needed to clear the $query string after each iteration as it was just adding each subsequent query to the previous query. Just added in $query = ""; at the beginning of the loop and now working a treat. ;0) Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 19, 2007 Share Posted November 19, 2007 Cool...please click "TOPIC SOLVED" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.