Jump to content

Error with Search and mysql_fetch_array


ScottNike

Recommended Posts

Getting the following error message: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in

 

This error occurs when I first navigate to the search page.  When I put a search term in the box and click search it works fine and the error is gone. I've been trying to figure this out for days with no luck, any help you can offer would be greatly appreciated. 

<?php
require('config.php');

?>

<?php

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
   or die('Could not connect to MySQL database. ' . mysql_error());
mysql_select_db(SQL_DB, $conn);

$result = NULL;

if (isset($_GET['keywords'])) {
  $sql = "SELECT id, hostname, ip, clust_name, clust_ip, node_name, ".
        "node_ip, os, patch " .
       "FROM serv_main " .
         "WHERE MATCH (hostname,ip) " .
         "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) " .
         "ORDER BY MATCH (hostname,ip) " .
         "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) DESC";

  $result = mysql_query($sql)
    or die('Could not perform search; ' . mysql_error());
   
}

?>
<html>
<body>
<form method="get" action="search.php">
  <p class="head">Type in the Server name below to search for an SLA</p>
  <p>
    <input id="searchkeywords" type="text" name="keywords"
<?php
if (isset($_GET['keywords'])) {
  echo ' value="' . htmlspecialchars($_GET['keywords']) . '" ';
}
?>
    >
    <input id="searchbutton" class="submit" type="submit"
      value="Search">
  </p>
<?php  

  echo "<h1>Search Results</h1>\n";

if ($result and !mysql_num_rows($result)) {

   if (isset($_GET['keywords'])) {  
      $k = $_GET['keywords'];
   }
   echo "<p>No SLA for <b><font size=\"6\" color=#990000>" . $k . "</b></font> found.</p>\n";
} else  {

   $bg = '';
//Error occurs right here*********************
   while ($row = mysql_fetch_array($result)) {
    //($row['id'], TRUE);
   
   $bg = ($bg=='F2F2FF'?'99CCCC':'F2F2FF');
      $table = "<table border=\"0\" cellpadding=\"5\">";
      $table .= "<tr bgcolor=\"#" . $bg . "\">" . 
         "<td><a href=\"servedit.php?s=" . $row['id'] . "\">" .
         $row['hostname'] . "</a></td><td>" .
         $row['ip'] . "</td><td align=\"center\">" .
         $row['clust_name'] . "</td><td>" .
         $row['clust_ip'] . "</td><td>" .
         $row['node_name'] . "</td><td>" .
         $row['node_ip'] . "</td><td>" .
         $row['os'] . "</td><td>" .
         $row['patch'] . "</td></tr>";
   }
   $table .= "</table>";
   echo $table;
   
}

?>
  </form>
  
  
  
</body>
</html>

 

MOD EDIT:

 . . . 

tags added.

Link to comment
Share on other sites

Thanks for the reply, I'm new to php and programming in general, so I really appreciate the help.  After the comment above, I was able to rework the code and figured it out.  Here's the code that worked:

 

<?php
require('config.php');

?>
<html>
<body>
<form method="get" action="search.php">
  <p class="head">Type in the Server name below to search for an SLA</p>
  <p>
    <input id="searchkeywords" type="text" name="keywords">
    <input id="searchbutton" class="submit" type="submit"
      value="Search">
</p>
</form>

<?php

?>
</body>
</html>
<?php

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die('Could not connect to MySQL database. ' . mysql_error());
mysql_select_db(SQL_DB, $conn);

$result = NULL;

if (isset($_GET['keywords'])) {
  $sql = "SELECT id, hostname, ip, clust_name, clust_ip, node_name, ".
     "node_ip, os, patch " .
	 "FROM serv_main " .
         "WHERE MATCH (hostname,ip) " .
         "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) " .
         "ORDER BY MATCH (hostname,ip) " .
         "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) DESC";

  $result = mysql_query($sql)
    or die('Could not perform search; ' . mysql_error());
$k = $_GET['keywords'];
echo "<h1>Search Results</h1>\n";	
if ($result and !mysql_num_rows($result)) {

echo "<p>No SLA for <b><font size=\"6\" color=#990000>" . $k . "</b></font> found.</p>\n";
} else  {

$bg = '';
while ($row = mysql_fetch_array($result)) {

$bg = ($bg=='F2F2FF'?'99CCCC':'F2F2FF');
	$table = "<table border=\"0\" cellpadding=\"5\">";
	$table .= "<tr bgcolor=\"#" . $bg . "\">" . 
		"<td><a href=\"servedit.php?s=" . $row['id'] . "\">" .
		$row['hostname'] . "</a></td><td>" .
		$row['ip'] . "</td><td align=\"center\">" .
		$row['clust_name'] . "</td><td>" .
		$row['clust_ip'] . "</td><td>" .
		$row['node_name'] . "</td><td>" .
		$row['node_ip'] . "</td><td>" .
		$row['os'] . "</td><td>" .
		$row['patch'] . "</td></tr>";
}
$table .= "</table>";
echo $table;

}	
}
?>

 

The code works as expected now, but just wondering if I should change anything else to improve the code.  Thanks again, very impressed with the site and peoples willingness to help!

Link to comment
Share on other sites

Your code looks alright to me, with the exception of your $_GET[] variables, they need to be filtered and validated. Other then that, practice makes perfect. This code can be significantly shortened and improved, but you will learn where and how you can improve it the more you dive into PHP. Good luck!

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.