Changed PDO connection page and received mysql_error ()

Iterated through my code to turn all my queries into prepared queries (PDOs). I changed the connection page accordingly. When I cleared the page of my functions, I ran into the error mysql_error() expects parameter 1 to be resource, integer given , which referred to my email function

 email_exists($email){ $query1 = mysql_query("SELECT COUNT(user_id) FROM tempusers WHERE `email` = '$email'") OR die(mysql_error(0)); $email = sanitize($email); return (mysql_result($query1,0)==1)? true : false; } 

Because this is a function page, meaning that I cannot call the variables on my connection page, combined with the fact that I am still stuck in the old query methods that I find it difficult to find. I would appreciate any ideas regarding the process of working with this request, as well as with any advice in general.

+4
source share
1 answer

You must handle errors in your function in the same way that you handle them everywhere.

However, since this is inside a function, you need to make the connection available within the scope of the function.

There are several ways to do this, some examples (from not so good for the better ...):

  • declare a PDO global connection variable in your function. I would not recommend this as a final solution, but it will work while you improve things;
  • add a PDO connection as an additional parameter to your function. Although better than 1., you will need to modify all function calls to include the new parameter (s);
  • refactor to OOP and use dependency injection to add your PDO connection to your email class / object.
+2
source

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


All Articles