Posted by Flamer on 10/08/15 11:53
shuterbug96@gmail.com wrote:
> I am trying to write a search string for a magazine site. it accesses
> the database but I am not getting results instead I am getting this:
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource in
> /www/currenthistory.com/htdocs/preview/search-results.php on line 32
>
> here are the lines:
> <?php
>
> $name = $_POST['keywords'];
> $name_arr = explode(' ', $name);
> $search_result = '';
>
> foreach($name_arr as $key => $name)
> {
> $query="SELECT * FROM Articls WHERE";
> $query.=" Keywords LIKE '%$name%'";
> $query.=" OR Title LIKE '%$name%'";
> $query.=" OR Author LIKE '%$name%' ORDER BY title";
> $result_arr[$key]=mysql_db_query("currenthistorydb", $query);
> $num_rows_arr[$key]=mysql_num_rows($result_arr[$key]);
> $search_result.= "Found $num_rows_arr[$key] results for the term
> $name.<br />";
> }
>
> mysql_close();
>
> echo($search_result);
>
> echo '<b><center><font size="4" color="#FF0000">Search
> Result</font></center></b><br><br>';
>
> foreach($result_arr as $key => $result)
> {
> $i=0;
> while ($i < $num_rows_arr[$key])
> {
>
> $row = mysql_fetch_row($result);
> $search_term = $name_arr[$key];
> $authors = $row[1];
> $title = $row[2];
> $source = $row[3];
>
> echo "<b>Search Term:</b> $search_term<br><b>Author:</b>
> $authors<br><b>Title:</b> $title<br><b>Source:</b>
> $source<br><br><hr><br>";
>
> $i++;
> }
> }
>
> ?>
>
>
> why is it giving me this error?
>
> any help will be much appreciated.
Is this line meant to have this spelling:
$query="SELECT * FROM Articls WHERE";
i think the real issue here is that mysql is not getting any results,
however good code should be able to handle that without creating an
error, like first checking if results where found, so after your select
statement if (!empty($mysql_query)) {//everything in here} else
{//sorry there was a problem accessing the database}
Flamer.
[Back to original message]
|