cs1h Posted October 11, 2007 Share Posted October 11, 2007 Hi, I have a script that is designed to perform a search and then show the results in pages of 5 results each but I am getting an error and I don't know how to solve it. Can anyone help? The error is, Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Inetpub\vhost\myroho.com\httpdocs\alimit2.php on line 137 And the script is, <?php $host = "localhost"; //your sql host, usually 'localhost' $user = "xxx"; //username to connect to database $pass = "xxx"; //password to connect to database $db = "xxx"; //the name of the database $targetb = $_POST['menuFilesDMA']; $targetb = str_replace(' ','_', $targetb); mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error()); mysql_select_db($db) or die("ERROR DB:".mysql_error()); $max = 5; //amount of articles per page. change to what to want $p = $_GET['p']; if(empty($p)) { $p = 1; } $limits = ($p - 1) * $max; //view the news article! if(isset($_GET['act']) && $_GET['act'] == "view") { $targetb = mysql_real_escape_string($targetb); $type = mysql_real_escape_string($_POST['Type']); $keywords = preg_split("/[\s,]+/", trim($_POST['keyword'])); $keys = ""; foreach($keywords as $K) { $keys .= "AND (Abstract LIKE '%$K%' OR town LIKE '%$K%')"; } $sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys ORDER BY id DESC"; while($r = mysql_fetch_array($sql)) { $Country = $row['country']; $Type = $row['type']; $More = $row['id']; $Title = $row['Title']; $Abs = $row['Abstract']; $Auth = $row['town']; echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td> <td height=\"28\" background=\"line_right_corner_top.png\"> </td> </tr> <tr> <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td> <td width=\"250\" height=\"21\"><span class=\"style42\">$Title</span></td> <td width=\"154\" height=\"21\"> </td> <td width=\"14\"> </td> <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td> <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td> </tr> <tr> <td height=\"86\" colspan=\"2\"><span class=\"style41\">$Abs</span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"250\" height=\"19\"><span class=\"style43\">$Auth</span></td> <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style39\">>></span> <span class=\"style40\"><a href=more.php?id=$More>Read More</a> </span> <span class=\"style39\">>></span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td> <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td> </tr> <tr> <td width=\"14\" height=\"19\"> </td> <td height=\"21\" colspan=\"4\"> </td> <td width=\"14\" height=\"19\"> </td> </tr> </table>"; } }else{ //view all the news articles in rows $targetb = mysql_real_escape_string($targetb); $type = mysql_real_escape_string($_POST['Type']); $keywords = preg_split("/[\s,]+/", trim($_POST['keyword'])); $keys = ""; foreach($keywords as $K) { $keys .= "AND (Abstract LIKE '%$K%' OR town LIKE '%$K%')"; } $sql = "SELECT * FROM items WHERE (country='$targetb' OR continent='$targetb') AND (type='$type' OR alltype='$type') $keys ORDER BY id DESC LIMIT ".$limits.",$max"; //the total rows in the table $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM jobs"),0); //the total number of pages (calculated result), math stuff... $totalpages = ceil($totalres / $max); //the table echo "<table><tr><td>Title</td><td>Author</td></tr><tr>"; while($r = mysql_fetch_array($sql)) { $Country = $row['country']; $Type = $row['type']; $More = $row['id']; $Title = $row['Title']; $Abs = $row['Abstract']; $Auth = $row['town']; echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td> <td height=\"28\" background=\"line_right_corner_top.png\"> </td> </tr> <tr> <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td> <td width=\"250\" height=\"21\"><span class=\"style42\">$Title</span></td> <td width=\"154\" height=\"21\"> </td> <td width=\"14\"> </td> <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td> <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td> </tr> <tr> <td height=\"86\" colspan=\"2\"><span class=\"style41\">$Abs</span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"250\" height=\"19\"><span class=\"style43\">$Auth</span></td> <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style39\">>></span> <span class=\"style40\"><a href=more.php?id=$More>Read More</a> </span> <span class=\"style39\">>></span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td> <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td> </tr> <tr> <td width=\"14\" height=\"19\"> </td> <td height=\"21\" colspan=\"4\"> </td> <td width=\"14\" height=\"19\"> </td> </tr> </table>"; for($i = 1; $i <= $totalpages; $i++){ //this is the pagination link echo "<a href='alimit2.php?p=$i'>$i</a>|"; } } } ?> Any help will be much appriciated, Thanks Colin Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/ Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 You haven't executed your query... You gave the sql string to mysql_fetch_array(). So before: while($r = mysql_fetch_array($sql)) You need to add: $sql = mysql_query($sql); Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366935 Share on other sites More sharing options...
cs1h Posted October 11, 2007 Author Share Posted October 11, 2007 Thanks that removed that error but I am now getting no results, the page just comes up with Title Author on it. Any ideas why this may be, Thanks Colin Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366942 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 Well obviously you have done something wrong in building the query, or simply by the data given there are no matches... Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366947 Share on other sites More sharing options...
kirk112 Posted October 11, 2007 Share Posted October 11, 2007 Where you added $sql = mysql_query($sql); change to $sql = mysql_query($sql) or die(mysql_error()); this will tell you if there are any problems with the sql query Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366948 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 @kirk112- if there was a problem with the query in it's syntax, mysql_query() would have returned false and then mysql_fetch_array() would spit an error that the given argument is not a mysql-object. So there's nothing wrong with the syntax, it's simply a problem of logic- the query is not built correctly. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366957 Share on other sites More sharing options...
adam291086 Posted October 11, 2007 Share Posted October 11, 2007 I am new at this so it may sound stupid, and if i am wrong can someone explain why. here goes while($r = mysql_fetch_array($sql)) You haven't defined what $r is. Therefore you are saying while the value of nothing execute the query. You need to define the value of $r. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366975 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 @adam- this is the syntax that is often being used to over the fetched rows. This is not a problem. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366983 Share on other sites More sharing options...
adam291086 Posted October 11, 2007 Share Posted October 11, 2007 Ok thanks for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/72759-help-with-search-results/#findComment-366987 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.