Jump to content

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.