0

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.

Thank you.

NullPoiиteя
  • 55,099
  • 22
  • 123
  • 139
randytan
  • 1,001
  • 5
  • 21
  • 49
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Jul 02 '13 at 13:43
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Jul 02 '13 at 13:43

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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
bwoebi
  • 23,302
  • 4
  • 56
  • 77
  • 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,316
  • 6
  • 25
  • 50
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
  • 1,968
  • 1
  • 13
  • 11