Warning: mysql_result () expects parameter 1 to be a resource, boolean

I know that we already have a lot of questions about this error, but I can’t fix my code, so anyone here, please help me fix this problem. my code is like this

function login($username, $password) { $user_id = user_id_from_username($username); $username = sanitize ($username); $password = md5 ($password); return (mysql_result(mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"),0)==1) ? $user_id : false; 

I look forward to hearing from you guys. I am new to this, so please keep your answer as simple as possible. thanks.

+5
source share
4 answers

Here is a small remake of your method.

! Important. MYSQL functions should be replaced with MYSQLi, since the MYSQL driver is deprecated after php 5.5 and removed in the new PHP7 ...

 function login($username, $password) { $username = sanitize ($username); $password = md5 ($password); $sql = "SELECT `user_id` FROM `users` WHERE `username` = '{$username}' AND `password` = '{$password}'"; $result = mysql_query($sql); if( $row = mysql_fetch_assoc($result)) { return $row['user_id']; } return FALSE; } 
0
source

mysql_query will return false on error and the resource will succeed. Before calling mysql_result, you must check the return value of mysql_query.

 $resource = mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'") or die(mysql_error()); if($resource !== false) { return (mysql_result($resource,0)==1) ? $user_id : false; } return false 

I would have avoided input too if you hadn't done it yet using mysql_real_escape_string ()

0
source

This error means that everything in mysql_result (HERE) returns false, not a resource!

You need var_dump, what end result you get in your request then try to execute through PMA through the console from mysql>
You will see that nothing is returned or an error in the request.

then fix your request and / or code and be cool!

If it’s too complicated, write your resul request in the comments, I will fix it!

0
source

Delete the last `at your request next to the false.

-1
source

Source: https://habr.com/ru/post/1236773/


All Articles