Help error when using php to search mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryan12324
Forum Newbie
Posts: 4
Joined: Sun Jun 07, 2009 3:00 pm

Help error when using php to search mysql database

Post 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>";
}
?>
 
Last edited by Benjamin on Sun Jun 07, 2009 11:29 pm, edited 2 times in total.
Reason: Changed code type from text to php.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Help error when using php to search mysql database

Post 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();
ryan12324
Forum Newbie
Posts: 4
Joined: Sun Jun 07, 2009 3:00 pm

Re: Help error when using php to search mysql database

Post by ryan12324 »

i still get the same error
kalebaustin
Forum Newbie
Posts: 12
Joined: Mon Nov 19, 2007 11:28 am

Re: Help error when using php to search mysql database

Post 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.
ryan12324
Forum Newbie
Posts: 4
Joined: Sun Jun 07, 2009 3:00 pm

Re: Help error when using php to search mysql database

Post by ryan12324 »

i now get this error
ERROR:Table 'a6756139_hexcode.news' doesn't exist
kalebaustin
Forum Newbie
Posts: 12
Joined: Mon Nov 19, 2007 11:28 am

Re: Help error when using php to search mysql database

Post by kalebaustin »

Alrighty, Now you know what to fix.
ryan12324
Forum Newbie
Posts: 4
Joined: Sun Jun 07, 2009 3:00 pm

Re: Help error when using php to search mysql database

Post 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
Post Reply