ScottNike Posted November 27, 2011 Share Posted November 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251876-error-with-search-and-mysql_fetch_array/ Share on other sites More sharing options...
MasterACE14 Posted November 27, 2011 Share Posted November 27, 2011 You have a logic error, look at where your if/else statements are and what they do, then you'll know where you currently have the mysql_fetch_array() does not make any sense. Quote Link to comment https://forums.phpfreaks.com/topic/251876-error-with-search-and-mysql_fetch_array/#findComment-1291516 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2011 Share Posted November 27, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/251876-error-with-search-and-mysql_fetch_array/#findComment-1291592 Share on other sites More sharing options...
ScottNike Posted November 27, 2011 Author Share Posted November 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/251876-error-with-search-and-mysql_fetch_array/#findComment-1291623 Share on other sites More sharing options...
MasterACE14 Posted November 28, 2011 Share Posted November 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/251876-error-with-search-and-mysql_fetch_array/#findComment-1291748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.