-5

Possible Duplicate:
supplied argument is not a valid MySQL result resource

I am trying to show results from my database on my site but I keep getting an error message or nothing shows.

The error message at the moment in time is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php on line 29

This is an updated version from my previous question as I have edited the php but it still doesnt work. What am I doing wrong?

James

<?php
// Change the fields below as per the requirements
$db_host="";
$db_username="u2";
$db_password="";
$db_name="";
$db_tb_name="";
$db_tb_atr_name[0]="name";
$db_tb_atr_name[1]="email";
$db_tb_atr_name[2]="location";


//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query_for_result=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("

SELECT $db_tb_atr_name[0], $db_tb_atr_name[1], $db_tb_atr_name[2]

FROM $db_tb_name WHERE 

$db_tb_atr_name[0], $db_tb_atr_name[1], $db_tb_atr_name[2] LIKE '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while ($data_fetch = mysql_fetch_array($query_for_result))
{
echo '<br>';
echo '<br>';
echo '<div class="data1">';
echo $data_fetch["name"];
echo '</div>';
echo '<br>';
echo '<div class="data2">';
echo $data_fetch["email"];
echo '</div>';
echo '<br>';
echo '<div class="data3">';
echo $data_fetch["location"];
echo '</div>';
}
echo "</ol>";

mysql_close();
?>
Community
  • 1
  • 1
Lizzie Moore
  • 41
  • 1
  • 3
  • 6
  • 1
    When you do database queries always add `or die( mysql_error() )` at the end so you'll see if there are errors in the query. – JJJ Nov 30 '11 at 14:22
  • 1
    You were already given an answer - http://stackoverflow.com/questions/8327003/keep-getting-the-same-mysql-error-how-do-i-overcome-this – matino Nov 30 '11 at 14:23
  • 1
    Ah, good old `"$var"` cargo-cult programming... – Marc B Nov 30 '11 at 14:44

2 Answers2

0

After running a mysql_* command that might get an error from the database, you can check the results of mysql_error(). In this case, it will tell you what went wrong with the mysql_query().

http://php.net/manual/en/function.mysql-error.php

Since mysql_query() will return FALSE on a SELECT statement with an error, you can do something like this:

$query  = "SELECT ... ";
$result = mysql_query($query);

if ($result === FALSE) {
    trigger_error(mysql_error()." in ".$query);
    exit();
}

You'll want to have similar error-checking for the mysql_connect() and mysql_db_select() calls as those can result in errors too.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Trott
  • 66,479
  • 23
  • 173
  • 212
  • Thanks! Im now getting: Parse error: syntax error, unexpected $end in /websites/123reg/LinuxPackage22/we/ez/y_/weezy.co.uk/public_html/search.php on line 63 but that line is just – Lizzie Moore Nov 30 '11 at 14:34
  • You've introduced a syntax error in your PHP. If you are using a decent IDE, it will hopefully highlight it for you. If you copied the `if` block above, make sure you included the closing `}`. – Trott Nov 30 '11 at 14:37
  • Thanks for the help so far! I just put it in but now it shows nothing its just a blank page – Lizzie Moore Nov 30 '11 at 14:44
  • This kind of error handling should be applied to ALL mysql statements, not just query calls. There's no point in checking if a query failed, if you haven't check if you successfully connected in the first place. – Marc B Nov 30 '11 at 14:46
  • @MarcB I've added a sentence at the end indicating that the error checking should be on all the mysql_* calls that might result in an error. Thanks for the comment! – Trott Nov 30 '11 at 14:51
  • I edited your code to make it less emotional but more informative and useful – Your Common Sense Nov 30 '11 at 15:35
  • @Marc these 2 functions have an output of their own, with sufficient error messages, while mysql_query haven't – Your Common Sense Nov 30 '11 at 15:38
  • @Col.Shrapnel I prefer the term "whimsical" to "emotional", but yes, your changes are an improvement for sure. Thanks for that! – Trott Nov 30 '11 at 17:00
  • i prefer trigger_error because it will act according to general error reporting rules of the site - display it, if you have display errors on and/or log it if you have log errors on. Not to mention thet it sets file and line stamps which is extremely useful – Your Common Sense Nov 30 '11 at 17:20
0

You might want to set $db_host = "localhost" and $db_name = "YOUR DATABASE NAME"

Clive
  • 36,918
  • 8
  • 87
  • 113
Superdooperhero
  • 7,584
  • 19
  • 83
  • 138