/    Sign up×
Community /Pin to ProfileBookmark

PHP search "mysql_num_rows()" problem

I’m trying to use search engine from tutorial I found, however there’s error with showing results on subpages. I got:

[CODE]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
[/CODE]

this line:

[CODE]
if($results = mysql_num_rows($sql_query) != 0)
[/CODE]

This is full code:

[code=php]
<?PHP
function getmicrotime()
{
list($usec, $sec) = explode(” “, microtime());
return ((float)$usec + (float)$sec);
}
//initializing connection to the database
$connection_string = dirname(__FILE__) . “/connectionstring.php”;

$hostname_MyConnectionPHP = “localhost”;
$database_MyConnectionPHP = “database”;
$username_MyConnectionPHP = “usrname”;
$password_MyConnectionPHP = “mypassword”;
$connections = mysql_connect($hostname_MyConnectionPHP, $username_MyConnectionPHP, $password_MyConnectionPHP) or die ( “Unabale to connect to the database” );

//selecting table
mysql_select_db(“test”) or die ( ‘Unable to select database.’ );

//max number of results on the page
$RESULTS_LIMIT=10;

if(isset($_GET[‘search_term’]) && isset($_GET[‘search_button’]))
{
$search_term = $_GET[‘search_term’];

if(!isset($first_pos))
{
$first_pos = “0”;
}

$start_search = getmicrotime();
//initializing MySQL Quary
$sql_query = mysql_query(“SELECT * FROM news WHERE MATCH(title,article) AGAINST(‘$search_term’)”);

//additional check. Insurance method to re-search the database again in case of too many matches (too many matches cause returning of 0 results)
if($results = mysql_num_rows($sql_query) != 0)
{
$sql = “SELECT * FROM news WHERE MATCH(title,article) AGAINST(‘$search_term’) LIMIT $first_pos, $RESULTS_LIMIT”;
$sql_result_query = mysql_query($sql);
}
else
{
$sql = “SELECT * FROM news WHERE (title LIKE ‘%”.mysql_real_escape_string($search_term).”%’ OR article LIKE ‘%”.$search_term.”%’) “;
$sql_query = mysql_query($sql);
$results = mysql_num_rows($sql_query);
$sql_result_query = mysql_query(“SELECT * FROM news WHERE (title LIKE ‘%”.$search_term.”%’ OR article LIKE ‘%”.$search_term.”%’) LIMIT $first_pos, $RESULTS_LIMIT “);
}

$stop_search = getmicrotime();
//calculating the search time
$time_search = ($stop_search – $start_search);
}
?>

<?PHP
if($results != 0)
{
?>
<!– Displaying of the results –>
<table border=”0″ cellspacing=”2″ cellpadding=”2″>
<tr>
<td width=”47%”>Results for <?PHP echo “<i><b><font color=#000000>”.$search_term.”</font></b></i> “; ?></td>
<td width=”53%” align=”right” height=”22″>Results <b>
<?PHP echo ($first_pos+1).” – “;
if(($RESULTS_LIMIT + $first_pos) < $results) echo ($RESULTS_LIMIT + $first_pos);
else echo $results ; ?>
</b>
out of <b><?PHP echo $results; ?></b>
for(<b><?PHP echo sprintf(“%01.2f”, $time_search); ?></b>)
seconds </td>
</tr>
<tr>
<form action=”” method=”GET”>
<td colspan=”2″ align=”center”> <input name=”search_term” type=”text” value=”<?PHP echo $search_term; ?>” size=”40″>
<input name=”search_button” type=”submit” value=”Search”> </td>
</form>
</tr>
<?PHP
while($row = mysql_fetch_array($sql_result_query))
{
?>
<tr align=”left”>
<td colspan=”2″><?PHP echo $row[‘title’]; ?></td>
</tr>
<?PHP
}
?>
</table>
<?PHP
}
//if nothing is found then displays a form and a message that there are nor results for the specified term
elseif($sql_query)
{

?>
<table border=”0″ cellspacing=”2″ cellpadding=”0″>
<tr>
<td align=”center”>No results for <?PHP echo “<i><b><font color=#000000>”.$search_term.”</font></b></i> “; ?></td>
</tr>
<tr>
<form action=”” method=”GET”>
<td colspan=”2″ align=”center”>
<input name=”search_term” type=”text” value=”<?PHP echo $search_term; ?>”>
<input name=”search_button” type=”submit” value=”Search”>
</td>
</form>
</tr>
</table>
<?PHP
}
?>
<table width=”300″ border=”0″ cellspacing=”0″ cellpadding=”0″>
<?php
if (!isset($_GET[‘search_term’])) { ?>
<tr>
<form action=”” method=”GET”>
<td colspan=”2″ align=”center”>
<input name=”search_term” type=”text” value=”<?PHP echo $search_term; ?>”>
<input name=”search_button” type=”submit” value=”Search”>
</td>
</form>
</tr>
<?php
}
?>
<tr>
<td align=”right”>
<?PHP

//displaying the number of pages where the results are sittuated

if($first_pos > 0)
{
$back=$first_pos-$RESULTS_LIMIT;
if($back < 0)
{
$back = 0;
}
echo “<a href=’search.php?search_term=”.stripslashes($search_term).”&first_pos=$back’ ></a>”;
}

if($results>$RESULTS_LIMIT)
{
$sites=intval($results/$RESULTS_LIMIT);
if($results%$RESULTS_LIMIT)
{
$sites++;
}
}
for ($i=1;$i<=$sites;$i++)
{
$fwd=($i-1)*$RESULTS_LIMIT;
if($fwd == $first_pos)
{
echo “<a href=’search.php?search_term=”.stripslashes($search_term).”&first_pos=$fwd ‘><b>$i</b></a> | “;
}
else
{
echo “<a href=’search.php?search_term=”.stripslashes($search_term).”&first_pos=$fwd ‘>$i</a> | “;
}
}

if(isset($first_pos) && $first_pos < $results-$RESULTS_LIMIT)
{
$fwd=$first_pos+$RESULTS_LIMIT;
echo “<a href=’search.php?search_term=”.stripslashes($search_term).”&first_pos=$fwd ‘ > >></a>”;
$fwd=$results-$RESULTS_LIMIT;
}
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscJul 11.2010 — Does the query work? Replace

[code=php]
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
[/code]


with

[code=php]
$sql_query = mysql_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')") or die(mysql_error());
[/code]


As an aside (a very important one), your script has a major security hole. You can't put user supplied data into a database query without sanitizing it first.

Do it by doing this:
[code=php]
$search_term = mysql_real_escape_string($_GET['search_term']);
[/code]


Note that without mysql_real_escape_string(), if you had a " or ' in $_GET['search_term'], then the query have a syntax error in it, so that may also have been your problem.
Copy linkTweet thisAlerts:
@HelleshternauthorJul 11.2010 — I think it's problem with pagination, because everything works till I'm trying show another result page (like results 10-20, 20-30 etc.).
Copy linkTweet thisAlerts:
@aj_nscJul 11.2010 — Edit: Nm
×

Success!

Help @Helleshtern spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...