p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: mysql_fetch_array()


Message #1 by "Jamal" <new_mojam@y...> on Thu, 17 Oct 2002 00:27:51
Hi Guys ,
Please Can anyone help me check my code below and tell me why I get the 
error message:"Warning: mysql_fetch_array(): supplied argument is not a 
valid MySQL result resource" ???.
Here is my code.


<?php

include "config.php";

$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* We now select one news post, and bring it into some text boxes */
$query = "SELECT title, news FROM news WHERE id = $_GET[id]";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{  
/* This bit sets our data from each row as variables, to make it easier to 
display */
$title=$r["title"];
$news=$r["news"];
/* Now lets display the titles */
    echo "<form name=\"edit_process.php\" method=\"post\" 
action=\"edit_save.php?id=$_GET[id]\">
  <p>Title : 
    <input type=\"text\" name=\"title\" value=\"$title\">
  </p>
  <p>News :</p>
  <p> 
    <textarea name=\"news\" cols=\"40\" rows=\"6\">$news</textarea>
  </p>
  <p>
    <input type=\"submit\" name=\"Submit\" value=\"Save\">
  </p>
</form>";
}
mysql_close($db);
?> 
Thanks in Advance.
Jamal
Message #2 by "Nikolai Devereaux" <yomama@u...> on Wed, 16 Oct 2002 16:23:02 -0700
That means that the database didn't return a valid result from your query.  I'd
suggest the following for debugging the problem:

$query = ...

$result = mysql_query($query) or die (mysql_error());


Again, I cannot stress enough the importance of error checking and validity
checking of function return values.

functions that return FALSE on failure explicitly do so to give you a way to
know if the function failed!

Message #3 by "Richard A. Holmes" <holmesra@w...> on Wed, 16 Oct 2002 17:41:45 -0600
Personally, ( I may be wrong here)
 but I would change
 while($r=mysql_fetch_arry($result))
 to
$nrows  = mysql_fetch_array($result) ;
 for ($i=0<$i<$nrows;$i++) {

 I use that code alot and am now moving to using
$nrows = mysql_fetch_object($result);

Hope that helps somewhat,
-Rich
----- Original Message -----
From: "Jamal" <new_mojam@y...>
To: "beginning php" <beginning_php@p...>
Sent: Thursday, October 17, 2002 12:27 AM
Subject: [beginning_php] mysql_fetch_array()


> Hi Guys ,
> Please Can anyone help me check my code below and tell me why I get the
> error message:"Warning: mysql_fetch_array(): supplied argument is not a
> valid MySQL result resource" ???.
> Here is my code.
>
>
> <?php
>
> include "config.php";
>
> $db = mysql_connect($db_host,$db_user,$db_pass);
> mysql_select_db ($db_name) or die ("Cannot connect to database");
> /* We have now connected, unless you got an error message */
> /* We now select one news post, and bring it into some text boxes */
> $query = "SELECT title, news FROM news WHERE id = $_GET[id]";
> $result = mysql_query($query);
> while($r=mysql_fetch_array($result))
> {
> /* This bit sets our data from each row as variables, to make it easier to
> display */
> $title=$r["title"];
> $news=$r["news"];
> /* Now lets display the titles */
>     echo "<form name=\"edit_process.php\" method=\"post\"
> action=\"edit_save.php?id=$_GET[id]\">
>   <p>Title :
>     <input type=\"text\" name=\"title\" value=\"$title\">
>   </p>
>   <p>News :</p>
>   <p>
>     <textarea name=\"news\" cols=\"40\" rows=\"6\">$news</textarea>
>   </p>
>   <p>
>     <input type=\"submit\" name=\"Submit\" value=\"Save\">
>   </p>
> </form>";
> }
> mysql_close($db);
> ?>
> Thanks in Advance.
> Jamal

Message #4 by "Nikolai Devereaux" <yomama@u...> on Wed, 16 Oct 2002 16:52:06 -0700
Hi Rich,

This code:

> $nrows  = mysql_fetch_array($result) ;
>  for ($i=0<$i<$nrows;$i++) {

Would still throw the error if $result isn't a valid link identifier.  Also,
$nrows is an array -- you're probably confusing mysql_fetch_array() for
mysql_num_rows().

To each their own -- the num_rows() way works, but many people prefer the
while() loop.

Here's how I do it:

$result = db_query(...);

if($result) // ensure a valid $result
{
   while($row = db_fetch_array($result, DB_ASSOC))
   {
      // do stuff with one row of result data here.
   }
}
else
{
   // handle bad result error here.
}


Alternatively, you can use negative logic to move the error handling up front:

if($result == FALSE)
{
   // handle bad result error here.
}
else
{
   while($row = db_fetch_array(...))
   {
      ...
   }
}



Take care,

Nik

Message #5 by "Richard A. Holmes" <holmesra@w...> on Wed, 16 Oct 2002 19:03:54 -0600
Thanks for the info nik
 This is a snippet of  some code i use for a bug tracking module


 The way I populate the $nrows is:
$result  = mysql_query("SELECT * from bug where status ='Open'") or
DIE("Could not execute query");
$nrows = mysql_num_rows($result);
for ($i=0;$i<$nrows;$i++)
{
   $row =mysql_fetch_array($result);
   extract($row);
 /-  Display the different variables below here -/

So far it seems to work smoothly but if you have a better or smoother way I
would greatly appreciate it.

Thanks again
-Rich

----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Wednesday, October 16, 2002 5:52 PM
Subject: [beginning_php] Re: mysql_fetch_array()


>
> Hi Rich,
>
> This code:
>
> > $nrows  = mysql_fetch_array($result) ;
> >  for ($i=0<$i<$nrows;$i++) {
>
> Would still throw the error if $result isn't a valid link identifier.
Also,
> $nrows is an array -- you're probably confusing mysql_fetch_array() for
> mysql_num_rows().
>
> To each their own -- the num_rows() way works, but many people prefer the
> while() loop.
>
> Here's how I do it:
>
> $result = db_query(...);
>
> if($result) // ensure a valid $result
> {
>    while($row = db_fetch_array($result, DB_ASSOC))
>    {
>       // do stuff with one row of result data here.
>    }
> }
> else
> {
>    // handle bad result error here.
> }
>
>
> Alternatively, you can use negative logic to move the error handling up
front:
>
> if($result == FALSE)
> {
>    // handle bad result error here.
> }
> else
> {
>    while($row = db_fetch_array(...))
>    {
>       ...
>    }
> }
>
>
>
> Take care,
>
> Nik
>
>

Message #6 by "Nikolai Devereaux" <yomama@u...> on Wed, 16 Oct 2002 18:08:37 -0700
I'm not sure if mysql_num_rows() throws an error when an invalid result is
given to it as a parameter.  If it doesn't now, there's no guarantee that it
won't later!

I still think the best thing to do is check for the validity of $result before
passing it to ANY function, be it num_rows(), fetch_array(), or whatever.

Message #7 by "Luis Morales" <luismorales@j...> on Wed, 16 Oct 2002 21:51:33 -0400
May be better if, evaluate the result after query.....

$result  = mysql_query("SELECT * from bug where status ='Open'") or
DIE("Could not execute query");

if ($result){
    $nrows = mysql_num_rows($result);
    for ($i=0;$i<$nrows;$i++)
    {
        $row =mysql_fetch_array($result);
        extract($row);
         /-  Display the different variables below here -/
    }
}
----- Original Message -----
From: "Richard A. Holmes" <holmesra@w...>
To: "beginning php" <beginning_php@p...>
Sent: Wednesday, October 16, 2002 9:03 PM
Subject: [beginning_php] Re: mysql_fetch_array()


> Thanks for the info nik
>  This is a snippet of  some code i use for a bug tracking module
>
>
>  The way I populate the $nrows is:
> $result  = mysql_query("SELECT * from bug where status ='Open'") or
> DIE("Could not execute query");
> $nrows = mysql_num_rows($result);
> for ($i=0;$i<$nrows;$i++)
> {
>    $row =mysql_fetch_array($result);
>    extract($row);
>  /-  Display the different variables below here -/
>
> So far it seems to work smoothly but if you have a better or smoother way
I
> would greatly appreciate it.
>
> Thanks again
> -Rich
>
> ----- Original Message -----
> From: "Nikolai Devereaux" <yomama@u...>
> To: "beginning php" <beginning_php@p...>
> Sent: Wednesday, October 16, 2002 5:52 PM
> Subject: [beginning_php] Re: mysql_fetch_array()
>
>
> >
> > Hi Rich,
> >
> > This code:
> >
> > > $nrows  = mysql_fetch_array($result) ;
> > >  for ($i=0<$i<$nrows;$i++) {
> >
> > Would still throw the error if $result isn't a valid link identifier.
> Also,
> > $nrows is an array -- you're probably confusing mysql_fetch_array() for
> > mysql_num_rows().
> >
> > To each their own -- the num_rows() way works, but many people prefer
the
> > while() loop.
> >
> > Here's how I do it:
> >
> > $result = db_query(...);
> >
> > if($result) // ensure a valid $result
> > {
> >    while($row = db_fetch_array($result, DB_ASSOC))
> >    {
> >       // do stuff with one row of result data here.
> >    }
> > }
> > else
> > {
> >    // handle bad result error here.
> > }
> >
> >
> > Alternatively, you can use negative logic to move the error handling up
> front:
> >
> > if($result == FALSE)
> > {
> >    // handle bad result error here.
> > }
> > else
> > {
> >    while($row = db_fetch_array(...))
> >    {
> >       ...
> >    }
> > }
> >
> >
> >
> > Take care,
> >
> > Nik
> >
> >
>
>
>

Message #8 by "Richard A. Holmes" <holmesra@w...> on Wed, 16 Oct 2002 19:56:38 -0600
Kewl
 Thanks for the Advice I really appreciate it
----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Wednesday, October 16, 2002 7:08 PM
Subject: [beginning_php] Re: mysql_fetch_array()


>
> I'm not sure if mysql_num_rows() throws an error when an invalid result is
> given to it as a parameter.  If it doesn't now, there's no guarantee that
it
> won't later!
>
> I still think the best thing to do is check for the validity of $result
before
> passing it to ANY function, be it num_rows(), fetch_array(), or whatever.
>
>

Message #9 by "Nikolai Devereaux" <yomama@u...> on Wed, 16 Oct 2002 19:03:44 -0700
> May be better if, evaluate the result after query.....
>
> $result  = mysql_query("SELECT * from bug where status ='Open'") or
> DIE("Could not execute query");
>
> if ($result){


The problem with this is that if $result is FALSE, then you'll exit the script
with your DIE() stmt before the if() expression is ever tested.

It's one or the other -- if you choose to die(), then don't bother with
if($result), since it's redundant, and wastes execution time testing a value
you already know is not FALSE.

take care,

nik

Message #10 by Kyle Ketterer <bige88fan@c...> on Wed, 16 Oct 2002 22:11:05 -0700
In your line:
SELECT title, news FROM news WHERE id = $_GET[id]";

Where you have your $_GEt variable declared try putting single quotes around
it like this:
 '$_GET[id]'

So now it looks like this:
SELECT title, news FROM news WHERE id = '$_GET[id]' ";

This may not be the problem however, you may not be selecting the right
tables in your DB. Hope this helps,

-Kyle.
----- Original Message -----
From: "Jamal" <new_mojam@y...>
To: "beginning php" <beginning_php@p...>
Sent: Wednesday, October 16, 2002 5:27 PM
Subject: [beginning_php] mysql_fetch_array()


> Hi Guys ,
> Please Can anyone help me check my code below and tell me why I get the
> error message:"Warning: mysql_fetch_array(): supplied argument is not a
> valid MySQL result resource" ???.
> Here is my code.
>
>
> <?php
>
> include "config.php";
>
> $db = mysql_connect($db_host,$db_user,$db_pass);
> mysql_select_db ($db_name) or die ("Cannot connect to database");
> /* We have now connected, unless you got an error message */
> /* We now select one news post, and bring it into some text boxes */
> $query = "SELECT title, news FROM news WHERE id = $_GET[id]";
> $result = mysql_query($query);
> while($r=mysql_fetch_array($result))
> {
> /* This bit sets our data from each row as variables, to make it easier to
> display */
> $title=$r["title"];
> $news=$r["news"];
> /* Now lets display the titles */
>     echo "<form name=\"edit_process.php\" method=\"post\"
> action=\"edit_save.php?id=$_GET[id]\">
>   <p>Title :
>     <input type=\"text\" name=\"title\" value=\"$title\">
>   </p>
>   <p>News :</p>
>   <p>
>     <textarea name=\"news\" cols=\"40\" rows=\"6\">$news</textarea>
>   </p>
>   <p>
>     <input type=\"submit\" name=\"Submit\" value=\"Save\">
>   </p>
> </form>";
> }
> mysql_close($db);
> ?>
> Thanks in Advance.
> Jamal
>


Message #11 by Cheng Gao <goghs@e...> on Thu, 17 Oct 2002 17:22:40 +0800
Hello Richard,

>$query = "SELECT title, news FROM news WHERE id = $_GET[id]";
I think you should write it as
$query = "SELECT title, news FROM news WHERE id = ".$_GET['id'];
or
$query = "SELECT title, news FROM news WHERE id = {$_GET['id']}";




-- 
Best regards,
Cheng Gao <goghs@e...>



Message #12 by "Nikolai Devereaux" <yomama@u...> on Thu, 17 Oct 2002 09:12:36 -0700
Hi Cheng,

> >$query = "SELECT title, news FROM news WHERE id = $_GET[id]";
> I think you should write it as
> $query = "SELECT title, news FROM news WHERE id = ".$_GET['id'];
> or
> $query = "SELECT title, news FROM news WHERE id = {$_GET['id']}";


All three lines are equivalent.  When dealing with arrays within double quoted
strings, the identifier inside the brackets is parsed as the array index.  If
the first character parsed is a digit, then it's treated as an integer index
into the array.  If it's an alpha character, it will be parsed as a string
index.

However, this only applies to one level of nesting in an array.  If you are
using a multidimensional array (e.g. $people[0]['name']), you have to either
exit the string (your 2nd line) or use curly brace syntax (your 3rd line).


Take care,

nik

Message #13 by "Luis Morales" <luismorales@j...> on Thu, 17 Oct 2002 22:23:08 -0400
You right Nik,

Obviusly on DIE $result has not efect.

I use the $query without Die, is better for me to take the control of the
aplication. :-)

Now the correct sample is....

$result  = mysql_query("SELECT * from bug where status ='Open'");

 if ($result){
     $nrows = mysql_num_rows($result);
     for ($i=0;$i<$nrows;$i++)
     {
         $row =mysql_fetch_array($result);
         extract($row);
          /-  Display the different variables below here -/
     }
}else{
   // take the control on Error case
}

best regards...

LM

> May be better if, evaluate the result after query.....
>
> $result  = mysql_query("SELECT * from bug where status ='Open'") or
> DIE("Could not execute query");
>
> if ($result){
>     $nrows = mysql_num_rows($result);
>     for ($i=0;$i<$nrows;$i++)
>     {
>         $row =mysql_fetch_array($result);
>         extract($row);
>          /-  Display the different variables below here -/
>     }
> }
> ----- Original Message -----
> From: "Richard A. Holmes" <holmesra@w...>
> To: "beginning php" <beginning_php@p...>
> Sent: Wednesday, October 16, 2002 9:03 PM
> Subject: [beginning_php] Re: mysql_fetch_array()
>
>
> > Thanks for the info nik
> >  This is a snippet of  some code i use for a bug tracking module
> >
> >
> >  The way I populate the $nrows is:
> > $result  = mysql_query("SELECT * from bug where status ='Open'") or
> > DIE("Could not execute query");
> > $nrows = mysql_num_rows($result);
> > for ($i=0;$i<$nrows;$i++)
> > {
> >    $row =mysql_fetch_array($result);
> >    extract($row);
> >  /-  Display the different variables below here -/
> >
> > So far it seems to work smoothly but if you have a better or smoother
way
> I
> > would greatly appreciate it.
> >
> > Thanks again
> > -Rich
> >
> > ----- Original Message -----
> > From: "Nikolai Devereaux" <yomama@u...>
> > To: "beginning php" <beginning_php@p...>
> > Sent: Wednesday, October 16, 2002 5:52 PM
> > Subject: [beginning_php] Re: mysql_fetch_array()
> >
> >
> > >
> > > Hi Rich,
> > >
> > > This code:
> > >
> > > > $nrows  = mysql_fetch_array($result) ;
> > > >  for ($i=0<$i<$nrows;$i++) {
> > >
> > > Would still throw the error if $result isn't a valid link identifier.
> > Also,
> > > $nrows is an array -- you're probably confusing mysql_fetch_array()
for
> > > mysql_num_rows().
> > >
> > > To each their own -- the num_rows() way works, but many people prefer
> the
> > > while() loop.
> > >
> > > Here's how I do it:
> > >
> > > $result = db_query(...);
> > >
> > > if($result) // ensure a valid $result
> > > {
> > >    while($row = db_fetch_array($result, DB_ASSOC))
> > >    {
> > >       // do stuff with one row of result data here.
> > >    }
> > > }
> > > else
> > > {
> > >    // handle bad result error here.
> > > }
> > >
> > >
> > > Alternatively, you can use negative logic to move the error handling
up
> > front:
> > >
> > > if($result == FALSE)
> > > {
> > >    // handle bad result error here.
> > > }
> > > else
> > > {
> > >    while($row = db_fetch_array(...))
> > >    {
> > >       ...
> > >    }
> > > }
> > >
> > >
> > >
> > > Take care,
> > >
> > > Nik
> > >
> > >
> >
> >
> >
>


  Return to Index