Does the connection job clear the selected database?

ABOUT,

this is really the bottom line where the question arises. But you may need other information after this, for example, how I connected, etc., to avoid misunderstandings. :)

$con = mysql_connect('localhost', 'root', '******'); if(!$con) { die('Could not establish connection: ' . mysql_error); } mysql_select_db('hunkusersystem'); function user_login($username, $password) { //Avoid SQL-injections. $username = mysql_real_escape_string($username); $password = md5($password); //Match user and password $sql = mysql_query("SELECT * FROM usersystem WHERE username = '$username' AND password = '$password' LIMIT 1", **$con**); 

My question is, in the very last block of code, I cannot use the resource identifier .. Why is this? Is it because mysql_select_db will be "cleaned up"? So, are you standing with the right connection, but without a database? If I used multiple connections, I must define the connection in mysql_select_db ();

Thanks so much for your help :) Greetings from Swe.

+4
source share
2 answers

This is a coverage issue; user_login cannot see $con because it was not passed as a parameter or declared as a global variable.

(Please do not declare it as a global variable, this is a very bad practice)

Try:

 function user_login($username, $password, $con) { 
+4
source

The $con variable is defined in the global scope, not in the local scope of the function.

If you want to use it there, you can use:

 function user_login($username, $password) { global $con; 

However, you better pass the variable to the function as a parameter.

The best solution would be to switch to PDO / mysqli with prepared instructions and use dependency injection, but not outside the scope of your question.

+2
source

All Articles