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? Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/ 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 Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394270 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! Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394275 Share on other sites More sharing options...
rlindauer Posted November 19, 2007 Share Posted November 19, 2007 Can you post the entire loop? Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394276 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. Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394278 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! Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394283 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."%'; Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394289 Share on other sites More sharing options...
chiprivers Posted November 19, 2007 Author Share Posted November 19, 2007 No probs in mysql! Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394297 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) Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394300 Share on other sites More sharing options...
wsantos Posted November 19, 2007 Share Posted November 19, 2007 Cool...please click "TOPIC SOLVED" Link to comment https://forums.phpfreaks.com/topic/77893-solved-mysql_fetch_array-throwing-up-error/#findComment-394307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.