-1
$update = mysql_query("UPDATE users SET regstatus='TRUE' WHERE username='$id' AND regstring='$key'") or die(mysql_error());
if (mysql_num_rows($check)>0)
    header("refresh:3;url=login.php");
    echo "Your account is active now. Redirecting to login page."; }

I get an output Your account is active now. Redirecting to login page and redirects me to the login page.

But the value of regstatus is not being updated. As a part of debugging, I check mysql_num_rows on the query, and it says a row is affected, but the value doesnt get updated still.

Any ideas? the column regstatus is varchar.

Updated the code as per suggestions :

 $update = mysql_query("UPDATE users SET regstatus='True' WHERE username='$id' AND regstring='$key'") or
        die(mysql_error());
    if (mysql_num_rows($update)>0)
    {
        header("refresh:3;url=login.php");
        echo "Your account is active now. Redirecting to login page.";
    }

Now I get :

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /activate.php on line 27

2
  • 1
    First check if ($update); then why do you use if (mysql_num_rows($check)? What is $check?
    – Marco
    Apr 12, 2012 at 9:44
  • Corrected. Please check the updated question again :)
    – Kishor
    Apr 12, 2012 at 9:52

5 Answers 5

6

Shouldn't it be

if (mysql_num_rows($update)>0) {
                    ^^^^^^     ^
2
  • 1
    @Kishor you havent corrected ... you replaced the function mysql_num_rows with mysql_affected_rows they take different arguments - either use mysql_affected_rows with your connection variable or mysql_num_rows with your result
    – Manse
    Apr 12, 2012 at 9:56
  • Can you check now again now please?
    – Kishor
    Apr 12, 2012 at 10:02
1

You want to use mysql_affected_rows() instead of mysql_num_rows(). Also, the argument should be $update instead of $check.

0
0

mysql_num_rows():

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

i.e. you have to use mysql_affected_rows in your code example

0
0

what is "$check" in the statement

 if (mysql_num_rows($check)>0)

shouldnt that be "$update"?

 if (mysql_num_rows($update)>0)   {
0
0

Have a look at mysql_affected_rows for getting the number of affected rows for the UPDATE query. You should also make clear what $check is.

In order to debug your MySQL query problem, try to debug print the query and to execute it manually, e.g. via PhpMyAdmin. Perhaps something is wrong with the parameters $id and $key.

2
  • Updated the question. I can see $id and $key has right values. But even if they have the wrong values, it should give the output without executing the actal query correctly. Wondering why.
    – Kishor
    Apr 12, 2012 at 10:05
  • Yes, that's because mysql_num_rows cannot get the number of affected rows for an UPDATE query. So, just replace mysql_num_rows($update) with mysql_affected_rows()
    – diewie
    Apr 12, 2012 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.