Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AND operator not working in a PHP function using MySQL

Tags:

php

mysql

I am using a function with MySQL to change password but the AND operator is not working properly.

CREATE DEFINER = 'root'@'%'
FUNCTION temp_staff.fn_changePassword(user_id INT, oldPassword VARCHAR(255), newPassword VARCHAR(255))
  RETURNS int(11)
BEGIN
update temp_staff.tbl_user set password = newPassword WHERE user_id = user_id AND password = oldPassword;
RETURN 1;
END

Result: it fetches 3 rows but in my table user_id is primary key, so give me proper solution.

like image 224
Rahul Borole Avatar asked Oct 30 '22 13:10

Rahul Borole


1 Answers

WHERE user_id = user_id will always evaluate to true (unless null). You should either rename the function parameter so that it doesn't conflict with the column name, or else table-qualify the reference to the table column.

like image 105
eggyal Avatar answered Nov 11 '22 10:11

eggyal