将 PDO 添加到我的代码时出错
我有connection.php 文件,我正在$db 中初始化PDO。 我想在 User.php 中检查此验证,我将其包含在 connection.php 之后。 但这给了我错误。
try {
$db = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username,$db_password);
echo "PDO connection object created";
}
catch(PDOException $e){
echo $e->getMessage();
}
我如何通过执行 PDO 来验证此代码。 我如何将 PDO 传递给用户类..
Fatal error: Call to a member function query() on a non-object in /var/www/youngib/rahul/yapi/user.php on line 41
$sql="select * from users where email='$this->email'";
$rs=$db->query($sql);
if(mysql_num_rows($rs)>0){
$msg=geterrormsg(4);
//email already exist
echo $msg= "{ 'success': 'false','msg':'$msg' ,'error_code':'4' }";
return false;
}
请帮助。
谢谢 。
I have connection.php file where i am initializing PDO in the $db.
And i want to check this validation in the User.php which i include after connection.php.
but it is giving me error .
try {
$db = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username,$db_password);
echo "PDO connection object created";
}
catch(PDOException $e){
echo $e->getMessage();
}
How can i validate this code by executing PDO.
How i will pass the PDO to the User Class..
Fatal error: Call to a member function query() on a non-object in /var/www/youngib/rahul/yapi/user.php on line 41
$sql="select * from users where email='$this->email'";
$rs=$db->query($sql);
if(mysql_num_rows($rs)>0){
$msg=geterrormsg(4);
//email already exist
echo $msg= "{ 'success': 'false','msg':'$msg' ,'error_code':'4' }";
return false;
}
Please Help.
Thanks .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其注入到类中或创建一个单例数据库类,例如...
注入:
单例:
Inject it in to the class or make a singleton DB class like...
Injection:
Singleton:
我不想这么说,但尝试
在
$db->query($sql);
行之前添加。它可能会起作用,具体取决于 $db 的创建位置。也就是说,prodigitalson 的答案是一种极大改进的方法,它只涉及修复整个设计,这涉及更多的前期工作:)
I hate to say this, but try just adding
before your
$db->query($sql);
line. It might work, depending on exactly where the $db was created.That said, prodigitalson's answer is a vastly improved approach, it just involves fixing your entire design, which involves more up front work :)