0

I keep getting a warning on my server after trying to do a search it says:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given inform/search.php on line 19

Heres the code, I don't quite understand why its not displaying the result. Any help would be much appreciated.

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['postcode'])){
  $postcode=$_POST['postcode'];
  //connect  to the database
  }
$con = mysql_connect('*****','*****','*****'); //connect to the database
if (!$con) //if cannot connect to the database, terminate the process
{
die('Could not connect: ' . mysql_error());
}
  $mydb=mysql_select_db("dataproj");
  //-query  the database table
  $sql="SELECT  * FROM companies WHERE postcode LIKE '%" . $postcode ."%'";
  //-run  the query against the mysql query function
  $result=mysql_query($sql);
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $postcode =$row['postcode'];
          $cname=$row['cname'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . $postcode . " " . $cname .  "</a></li>\n";
  echo "</ul>";
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
?>
  • Can you give us a little bit more information? – Frosty Jun 15 '15 at 10:31
  • youre query has failed and is therefore false. debug the query and youll fix the `fetch_array`. You should also be using mysqli (or PDO) with prepared statements., mysql is deprecated and insecure. http://php.net/manual/en/mysqli.prepare.php – DevDonkey Jun 15 '15 at 10:32
  • that type of situation occurs when query failed to execute. Try outputting "$sql" and run that query directly in phpMyAdmin or sqlyyog. You will be able to get the error. – joy d Jun 15 '15 at 10:32
  • your query failed to fetch the result check the error using mysql_error() api – Sundar Jun 15 '15 at 10:32

1 Answers1

0

Firstly don't use the mysql API it is deprecated use PDO or mysqli.

Secondly Check $result before passing it to mysql_fetch_array. You'll find that it's false because the query failed. See the mysql_query documentation for possible return values and suggestions for how to deal with them.

<?php

if (isset($_POST['submit'])) {
    if (isset($_GET['go'])) {
        if (preg_match("/^[  a-zA-Z]+/", $_POST['postcode'])) {
            $postcode = $_POST['postcode'];
            //connect  to the database
        }
        $con = mysql_connect('*****', '*****', '*****'); //connect to the database
        if (!$con) { //if cannot connect to the database, terminate the process
            die('Could not connect: ' . mysql_error());
        }
        $mydb = mysql_select_db("dataproj");
        //-query  the database table
        $sql = "SELECT  * FROM companies WHERE postcode LIKE '%" . $postcode . "%'";
        //-run  the query against the mysql query function
        $result = mysql_query($sql);
        //check here
        if ($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $postcode = $row['postcode'];
            $cname = $row['cname'];
            //-display the result of the array
            echo "<ul>\n";
            echo "<li>" . $postcode . " " . $cname . "</a></li>\n";
            echo "</ul>";
        }
    } else {
        echo "<p>Please enter a search query</p>";
    }
}
?>
Faiz Rasool
  • 1,396
  • 12
  • 20