0

Hi there,

I have a problam with my code, and i cant fix it, please help.
The problam is with the mysql_num_rows on line 4 ;
Here is the code:

$sql_query = mysql_query("SELECT * FROM upload WHERE MATCH(username,text) 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) //Error on this line !!!!
            {
                $sql =  "SELECT * FROM upload WHERE MATCH(username,subject) AGAINST('$search_term') LIMIT $first_pos, $RESULTS_LIMIT";
                  $sql_result_query = mysql_query($sql);         
            }

Thanks for your help!

Edited by Harry_5

5
Contributors
8
Replies
40
Views
3 Years
Discussion Span
Last Post by diafol
1

Usually this happends because your query failed. If mysql_query fails it returns FALSE so $sql_query would end up being a boolean vice a object resource. Add a die statement to your query to see what gets posted back to you. You should surround your variable assignment into parenthesis as well:

$sql_query = mysql_query("SELECT * FROM upload WHERE MATCH(username,text) AGAINST('$search_term')") or die(mysql_error());

results = mysql_num_rows($sql_query)) !== 0)
0

Hi thanks for that jovstudios, but sorry that didnt change a thing,

And GliderPilot, it has impvoreved it it think, but i know get this error, "Parse error: syntax error, unexpected T_IS_NOT_IDENTICAL"

If you need ant more code, here is more:

 $start_search = getmicrotime();
      //initializing MySQL Quary  
    $sql_query = mysql_query("SELECT * FROM upload WHERE MATCH(username,text) 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) //here!!!!
            {
                $sql =  "SELECT * FROM upload WHERE MATCH(username,subject) AGAINST('$search_term') LIMIT $first_pos, $RESULTS_LIMIT";
                  $sql_result_query = mysql_query($sql);         
            }
    else
            {
                  $sql = "SELECT * FROM upload WHERE (username LIKE '%".mysql_real_escape_string($search_term)."%' OR text LIKE '%".$search_term."%') ";
                  $sql_query = mysql_query($sql);
                  $results = mysql_num_rows($sql_query);
                  $sql_result_query = mysql_query("SELECT * FROM upload WHERE (username LIKE '%".$search_term."%' OR text LIKE '%".$search_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
            }

Thanks for you help, but please help!!!

Edited by Harry_5

0

Try to use mysql_affected_rows() instead of mysql_num_rows()

Don't do this - that's for insert, update, delete etc - changes to the data - not "select".

if($results = mysql_num_rows($sql_query)) !== 0) //here!!!!

chnage to

if(mysql_num_rows($sql_query))

there may be other issues

2

if($results = mysql_num_rows($sql_query)) !== 0) //here!!!!

That line is throwing an error because you have closed both parenthesis, but then continued on to use a conditional operator. It looks to me that you attempted to do this:

if (($results = mysql_num_rows($sql_query)) !== 0)

Perhaps most importantly: I would strongly encourage you to stop using the original MySQL extension. As of PHP 5.5.0 the extension has been deprecated. It has not been actively developed in quite a few years. MySQLi or PDO are the recommended alternatives.

0

Yes, I originally typed if (($results = mysql_num_rows($sql_query)) !== 0) for him, not sure what happened to the first few characters when I posted it.

0

Ok thanks for the help guys but i am still getting this error,
even thaugh i have changed the code to this,

if (($results = mysql_num_rows($sql_query)) !== 0)

I'm still getting this error message,
Warning: mysql_num_rows() expects parameter 1 to be resource,
Please help but thanks for the help so far guys

Edited by Harry_5

0

Did you try

if(mysql_num_rows($sql_query))

If num rows is 0, then if(0) = false, else it's true, so works.

if (($results = mysql_num_rows($sql_query)) !== 0)

You don't seem to use $results after this, so I don't see why you need the assignment.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.