-1

I have some PHP problems regarding my PHP code

I create function that update database, for changing password. Here's my syntax

function changePassword($username, $password, $salt){   
$query = "UPDATE mt_user SET password = '". $password ."' , salt = '". $salt . "' WHERE username = '". $username ."'";
$result = mysql_query($query);
    if ($result == false){
        $num_rows = mysql_error();
    } else {
        $num_rows = mysql_num_rows($result);
    }
mysql_close();
return $num_rows;
}

I try this function by create some script:

    echo changePassword('user1','test','test_salt');

The database value is updated but, the function is showing some warnings

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in.....

What's wrong with the code? Because I don't see any errors.

Dharman
  • 30,962
  • 25
  • 85
  • 135
randytan
  • 1,029
  • 5
  • 23
  • 52

3 Answers3

3

mysql_num_rows() is the wrong function here because what is does

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 see how many rows were changed, use mysql_affected_rows().

$num_rows = mysql_affected_rows();

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • hi, thanks for your information, yes i know i need to upgrade, but i still curious why this function is working but shows some warnings., now it's warning: Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given – randytan Jun 30 '13 at 12:25
  • @randytan don't pass any parameter to mysql_affected_rows… – bwoebi Jun 30 '13 at 12:25
  • i just change into mysql_affected_rows($result); the $num_rows is deleted, but still some warnings given – randytan Jun 30 '13 at 12:28
  • @randytan ___no___ parameters: `mysql_affected_rows();` (don't pass `$result`.) – bwoebi Jun 30 '13 at 12:29
  • i don't understand, please give some examples. – randytan Jun 30 '13 at 12:31
  • @randytan do ___not___ write `$num_rows = mysql_affected_rows($result);`. Write: `$num_rows = mysql_affected_rows();`! – bwoebi Jun 30 '13 at 12:32
1

for update and insert queries you need to use mysql_affected_rows. mysql_num_rows only works for select statement.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

A little advice: replace mysql to mysqli. It's more secure. This example is with this one.

function changePassword($username, $password, $salt){   
$query = "UPDATE mt_user SET password = '". $password ."' , salt = '". $salt . "' WHERE username = '". $username ."'";
$result = mysqli_query($connection,$query);
    if ($result){
    $num_rows = mysqli_affected_rows($connection);        
    } else {
        $num_rows = mysqli_error($connection);
    }
mysql_close();
return $num_rows;
}
phsaires
  • 2,188
  • 1
  • 14
  • 11