Question

Was going over my code to turn all my queries into prepared queries (PDO). I changed my connection page accordingly. As I was cleaning up my functions page I came across 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 it is a function page meaning I can't call on the variables in my connection page coupled with the fact I'm still stuck in the old query ways I'm having a hard time fixing this query. I'd appreciate any ideas on the process of making this query work as well as any tips in general.

Était-ce utile?

La solution

You should handle errors in your function the same way you handle them everywhere else.

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

There are several ways to do that, some examples (from not that good to better...):

  1. declare your PDO connection variable global in your function. I would not recommend this as a final solution, but it would work while you improve on things;
  2. add the PDO connection as an additional parameter to your function. Although better than 1., you would need to change all function calls to include the new parameter(s);
  3. refactor to OOP and use dependency injection to add your PDO connection to your email class / object.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top