0

Let me begin by apologizing if there was a duplicate of this error. I searched and found nothing explicitly pertaining to my situation, though the same error message was presented. I'm new to PHP, and I'm attempting to use an HTML Search form in order to retrieve a record in my table.My code looks like this`

  <form action="" method="post">
    Search: <input type="text" name="term" /><br />
  <input type="submit" name="submit" value="Submit" />
  </form>

  <?php
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = 'pass';
  $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    if(! $conn ){
      die('Could not connect: ' . mysql_error());
  }

  if (!empty($_REQUEST['term'])) {

    $term = mysql_real_escape_string($_REQUEST['term']);

    $sql = "SELECT * FROM shoes WHERE name LIKE '%".$term."%'";
    $r_query = mysql_query($sql);

    while($row = mysql_fetch_array($r_query)){
      echo 'Shoe Name: ' .$row['name'];
      echo '<br /> SKU: ' .$row['sku'];
      echo '<br /> Size: ' .$row['size'];
      echo '<br /> Quantity: ' .$row['quantity'];
    }

  }
    mysql_close($conn);


  ?>

` And i get this error message: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I'd greatly appreciate any help on the matter. I'm stumped.

GavinB
  • 88
  • 1
  • 5

2 Answers2

0

In case of an error the mysql_query function return false. You might want to add some error checking eg.

if ($r_query === false){
    echo 'Error'; 
    // maybe check mysql_error* functions
    die ();
}
fejese
  • 4,481
  • 4
  • 27
  • 36
0

You haven't defined nor selected your database using mysql_select_db(), and also not passing DB connection to your query.

<?php
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = 'pass';
  $db = 'your_database'; // added. Modify to be your database
  $conn = mysql_connect($dbhost, $dbuser, $dbpass);

  mysql_select_db($db)or die(mysql_error());

    if(! $conn ){
      die('Could not connect: ' . mysql_error());
  }

    if (!empty($_REQUEST['term'])) {

    $term = mysql_real_escape_string($_REQUEST['term']);

    $sql = "SELECT * FROM shoes WHERE name LIKE '%".$term."%'";
    $r_query = mysql_query($sql,$conn); // added DB connection

    while($row = mysql_fetch_array($r_query)){
      echo 'Shoe Name: ' .$row['name'];
      echo '<br /> SKU: ' .$row['sku'];
      echo '<br /> Size: ' .$row['size'];
      echo '<br /> Quantity: ' .$row['quantity'];
    }

  }
    mysql_close($conn);
?>

Quoting from PHP.net

http://www.php.net/mysql_query

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.


http://php.net/manual/en/language.types.resource.php

Resources

A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132