将连接页面更改为PDO并获得mysql_error() [英] Changed connection page to PDO and got mysql_error()

查看:61
本文介绍了将连接页面更改为PDO并获得mysql_error()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在遍历我的代码以将我的所有查询都转换为准备好的查询(PDO).我相应地更改了连接页面.在清理函数页面时,我遇到了错误mysql_error() expects parameter 1 to be resource, integer given,它涉及我的电子邮件功能

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.

推荐答案

您应该以与处理其他所有地方相同的方式来处理函数中的错误.

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. 在函数中声明全局的PDO连接变量.我不建议您将其作为最终解决方案,但是在您进行改进时,它会起作用;
  2. 将PDO连接添加为函数的附加参数.尽管比1更好,但是您需要更改所有函数调用以包括新参数;
  3. 重构为OOP并使用依赖注入将PDO连接添加到电子邮件类/对象.

这篇关于将连接页面更改为PDO并获得mysql_error()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆