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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!';
}
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!';
}
}
?>

Link to comment
Share on other sites

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!';
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.