Jump to content

[SOLVED] Simple search form giving warning


Presto-X

Recommended Posts

Hello guys,

 

I'm working on a very basic page, I want to search one table in my database I was thinking this was going to be simple but I keep getting this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /volume1/web/mangos/templates/search.php on line 11

 

This is my code

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

<?PHP
mysql_connect("localhost", "XXXXX", "XXXXX"); 
mysql_select_db("XXXXX"); 
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_tempate WHERE name LIKE '%$search%'");
while($r=mysql_fetch_array($result))
{
  $name=$r["name"];
  echo "$name<br>";
}
?>

 

What am I missing?

This means that either your query is failing, or there are no rows returned. Try modifying your query to this:

 

 

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

<?php
mysql_connect("localhost", "XXXXX", "XXXXX"); 
mysql_select_db("XXXXX");
if(isset($_POST['submit'])){
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_tempate WHERE name LIKE '%$search%'") or die(mysql_error());
$num = mysql_num_rows($result);
if($result > 0){
    while($r=mysql_fetch_array($result))
    {
        $name=$r["name"];
        echo "$name<br>";
    }
}else{
    echo 'No matching items found!';
}
}
?>

 

You'll notice i've also added something to ensure the query is only perfomed once the search button has been pressed.

Hey Ben,

 

Thanks for the reply, that took care of the warning, but it does not display anything even if it did not find anything? I also found a misspelling on my part for the table name "item_tempate" should be "item_template". But it's not returning any results.

Is there any tag that I can place in here to make sure it’s connecting to the database and to the table ok?

 

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

<?php
mysql_connect("localhost", "XXXXX", "XXXXX"); 
mysql_select_db("XXXXX");
if(isset($_POST['submit'])){
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error());
$num = mysql_num_rows($result);
if($result > 0){
    while($row=mysql_fetch_array($result))
    {
        $name=$row["name"];
        echo "$name<br>";
    }
}else{
    echo 'No matching items found!';
}
}
?>

Sorry, that would be my fault - didn't notice you'd capitalized the first letter of the name of the submit button - hence the if(isset()) was always failing. Try:

 

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

<?php
mysql_connect("localhost", "XXXXX", "XXXXX"); 
mysql_select_db("XXXXX");
if(isset($_POST['Submit'])){
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error());
if(mysql_num_rows($result) > 0){
    while($r=mysql_fetch_array($result))
    {
        $name=$r["name"];
        echo "$name<br>";
    }
}else{
    echo 'No matching items found!';
}
}
?>

 

change

$num = mysql_num_rows($result);
if($result > 0){

 

to

if(mysql_num_rows($result) > 0){

 

Indeed - not sure where that came from!

humm, still nothing  :-\

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

<?php
mysql_connect("localhost", "XXXX", "XXXX"); 
mysql_select_db("mangos");
if(isset($_POST['submit'])){
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error());
if(mysql_num_rows($result) > 0){
    while($row=mysql_fetch_array($result))
    {
        $name=$row["name"];
        echo "$name<br>";
    }
}else{
    echo 'No matching items found!';
}
}
?>

Nice that capital S did the trick,

 

Thanks Ben.

 

This is the working code in case someone else finds this useful

 

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

<?php
mysql_connect("localhost", "USERNAME", "PASSWORD"); 
mysql_select_db("DATABASE");
if(isset($_POST['Submit'])){
$search=$_POST["search"];
$result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error());
if(mysql_num_rows($result) > 0){
    while($row=mysql_fetch_array($result))
    {
        $name=$row["name"];
        echo "$name<br>";
    }
}else{
    echo 'No matching items found!';
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.