Page 1 of 1

Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:06 pm
by ryan12324
i have a website that i am making and i want to search a my sql table but it comes up with an error.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a6756139/public_html/search.php on line 17


please help code is below.

Form Code

Code: Select all

<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>
search.php

Code: Select all

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","username","password"); 
    
//select which database you want to edit
mysql_select_db("a6756139_hexcode"); 
 
$search=$_POST["search"];
 
//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");
 
//grab all the content
while($r=mysql_fetch_array($result))
{   
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $title=$r["title"];
   $message=$r["message"];
   $who=$r["who"];
   $date=$r["date"];
   $time=$r["time"];
   $id=$r["id"];
   
   //display the row
   echo "$title <br> $message <br> $who <br> $date | $time <br>";
}
?>
 

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:14 pm
by Darhazer
It tells you that the query was not executed successfully.
So you have to check results of the queries you are performing
after

Code: Select all

$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");
Add:

Code: Select all

if (!$result) echo mysql_error();

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:32 pm
by ryan12324
i still get the same error

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:34 pm
by kalebaustin
I'd remove your mysql credentials there

And try this:

Code: Select all

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("host","username","password") or die("ERROR:" . mysql_error() );
   
//select which database you want to edit
mysql_select_db("a6756139_hexcode") or die("ERROR:" .  mysql_error() );
 
$search=$_POST["search"];
 
//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'") or die("ERROR:" . mysql_error() );
 
//grab all the content
while($r=mysql_fetch_array($result))
{   
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
 
   $title=$r["title"];
   $message=$r["message"];
   $who=$r["who"];
   $date=$r["date"];
   $time=$r["time"];
   $id=$r["id"];
   
   //display the row
   echo "$title <br> $message <br> $who <br> $date | $time <br>";
}
?>
I added OR DIE(mysql_error() ) after the mysql functions
This will tell you where the script is failing, and the mysql error that is causing it.

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:43 pm
by ryan12324
i now get this error
ERROR:Table 'a6756139_hexcode.news' doesn't exist

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:44 pm
by kalebaustin
Alrighty, Now you know what to fix.

Re: Help error when using php to search mysql database

Posted: Sun Jun 07, 2009 3:55 pm
by ryan12324
ok ive fixed that but now i get this error
ERROR:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EditorID LIKE '%ammo%'' at line 1