Jump to content

[SOLVED] mysql_fetch_array throwing up error


chiprivers

Recommended Posts

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?

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!

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!

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."%';

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.