PDO对象的麻烦 [英] trouble with PDO object

查看:44
本文介绍了PDO对象的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我正在创建一个会员网站,我只有两个类,一个用于连接数据库的类和一个用于管理用户的类

so I am creating a membership website so far I only have 2 classes a class for connecting got the database and a class to manage users

这是数据库的类:

class dbConnect
{
  protected $db_conn;
  public $db_host = '127.0.0.1';
  public $db_user = 'root';
  public $db_pass = '';
  public $db_name = 'db';

  public function connect()
  {
    try
    {
      $this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass);
      return $this->db_conn;
    }
    catch(PDOException $e) 
    {
      return $e->getMessage();
    }
  }

}

这里是用户类,它只有一种方法,我刚刚启动了这个项目

Here is the users class it only has 1 method I just started this project

class ManageUsers
{
  public $link;

  function __construct()
  {
    $db_connection = new dbConnect();
    $this->link = $db_connection->connect();
    return $this->link;
  }

  function registerUsers($username,$email,$password,$ip_adress,$date)
  {
    $query = $this->link->prepare("INSERT INTO `users` (username,email,password,ip_adress,date_joined) VALUES(?,?,?,?,?)");
    $values = [$username,$email,$password,$ip_adress,$date];
    $query->execute($values);
    $confirm = $query->rowCount();
    return $confirm;
  }
}

现在我正在运行一个测试对象,以查看所有这些功能是否正常运行

Now I'm just running a test object to see if all this s functioning

$test = new ManageUsers();
echo $test->registerUsers('bob','a@a.com','lol','127.0.0.1','2012');

现在,我收到错误消息,称我对非对象调用prepare语句.老实说,我不明白,因为我在Construct方法中创建了对象.好任何建议都可以帮助谢谢!

now I am getting the error that I am calling the prepare statement to a non object. Which to be honest I don't understand since I creating the object in the construct method. well any advice helps Thanks!

推荐答案

我认为PDO对象无效,因为您使用带双引号的对象的方法/属性,在这种情况下,您需要使用复杂的字符串符号("{$object->property}" )或使用.(点)

I think PDO object is invalid, because you are using double quotes with method/property of object, in that case you need use complex string notation("{$object->property}") or join strings with . (a dot)

关于php字符串

$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass);

更改为:

$this->db_conn = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user,$this->db_pass);

这篇关于PDO对象的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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