Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue with simple Select for MySql

Status
Not open for further replies.

hovercraft

Technical User
Jun 19, 2006
236
US
Greetings,
I keep getting this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/panthert/public_html/rikenkaki/check_edit02.php on line 45

When running this code:

<?php
$checkername=$_POST['checkername'];
echo $checkername.'<br>';


$host="xxxxxx";
$user="xxxxxxx";
$password="xxxxx";
$database="xxxxxxxx";

$Link_ID=mysql_pconnect($host, $user, $password);
if (!$Link_ID)
{
echo "Failed to connect to Server=".$host;
return 0;
}
else
{
echo "Successfully connected to Server=".$host;
}


if (!@mysql_select_db($database,$Link_ID))
{
echo "
Cannot use database=".$database;
}
else
{
echo "
Successfully connected to database=".$database;
}

echo '<br>';
$rs = mysql_query("SELECT * FROM inv_checker WHERE inv_checker.check_checknam = '$checkername';");


while ( $row = mysql_fetch_array($rs) )
{
echo ($row["checker_checkid"].'&nbsp;'.$row["checker_checknam"]);
}
?>

I'm guessing that the error orginates in the $rs variable, specifically in the WHERE clause.
but I'm stumped.

The variable = $checkername is populated from a previous php listbox. I verify that the variable does have a value by echoing it. That works fine. So it must be in my syntax.
Any help would be greatly appreciated.

Thanks,
Dave
 
I don't mean to offend anyone. However....I am a big tard!

The field name was WRONG!!!, I corrected the sql string field name and also changed some of the syntax to:


$rs = mysql_query("SELECT * FROM inv_checker WHERE checker_checknam = '".$checkername."';");


(note correct field name) Duh....

Thanks anyways and sorry for not triple checking my own code.
 
One thing you can do to make life easier for yourself in future is to get in the habit of checking for and reacting to errors.

At absolute minimum, changing this line:

$rs = mysql_query("SELECT * FROM inv_checker WHERE inv_checker.check_checknam = '$checkername';");

To read:

$rs = mysql_query("SELECT * FROM inv_checker WHERE inv_checker.check_checknam = '$checkername';") [red]or die(mysql_error())[/red];

would have given you something to start with when debugging.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for the advice and the code.

That's one of my weak areas. -error handling
I must make an attempt to add it more often.

What is a good rule of thumb for placing such error traps? Would you suggest after every mysql call?


Thanks again,
Dave
 
The rule of thumb is pretty simple. In production code that you intend users to use, when your code intends to use a varibable it must make sure the variable was set with the value you expect.

I will output a general error-message for the user and log debugging information for my benefit to a logfile. Typically, my production PHP code to fetch information from a MySQL datbase reads something like:

Code:
$dbh = mysql_connect (...);

if ($dbh !== FALSE)
{
   $result = mysql_select_db('dbname', $dbh);

   if ($result !== FALSE)
   {
      $query = "SELECT.....";

      $rh = mysql_fetch_assoc ($query, $dbh);

      if ($rh !== FALSE)
      {
         //produce output based on fetched data
      }
      else
      {
         //output generic error message for user
         //log error message
      }
   }
   else
   {
      //output fairly generic error message for user
      //log "could not select database" error to logfile
   }
}
else
{
   //output fairly generic error message for user
   //log "could not connect" error message
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top