学习 PHP 中的 OOP,尝试重构我的代码。无法再连接到数据库

发布于 2024-09-03 13:59:49 字数 1162 浏览 5 评论 0原文

我以为我理解了类是如何工作的,然后我尝试了这段代码:

class user
  {
  var $dbcon;

  var $dbinfo;
  var $con;    
  var $error;

  function dbConnect()
    {

    $this->dbinfo['server'] = "localhost";
    $this->dbinfo['database'] = "foolish_faith";
    $this->dbinfo['user'] = "user";
    $this->dbinfo['password'] = "password";

    $this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
    $this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
    $this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->error = $this->dbcon->errorInfo();

    if ($error[0] != "")
      {
      print "Error!";
      print_r($error);
      }
    }
  }

现在它只是吐出这个错误:

致命错误:未捕获的异常 “PDOException”,消息“无效” 数据源名称' in E:\PortableApps\xampp\htdocs\dbcon.php:24 堆栈跟踪:#0 E:\PortableApps\xampp\htdocs\dbcon.php(24): PDO->__construct('', NULL, NULL) #1 E:\PortableApps\xampp\htdocs\login.php(4): user->dbConnect() #2 {main} 抛出 E:\PortableApps\xampp\htdocs\dbcon.php 第 24 行

有人能看出我做错了什么吗,因为我确信这与我在课堂上缺乏知识有关?

Thought I understood how classes work, then I tried this code:

class user
  {
  var $dbcon;

  var $dbinfo;
  var $con;    
  var $error;

  function dbConnect()
    {

    $this->dbinfo['server'] = "localhost";
    $this->dbinfo['database'] = "foolish_faith";
    $this->dbinfo['user'] = "user";
    $this->dbinfo['password'] = "password";

    $this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
    $this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
    $this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->error = $this->dbcon->errorInfo();

    if ($error[0] != "")
      {
      print "Error!";
      print_r($error);
      }
    }
  }

Now it just spits out this error:

Fatal error: Uncaught exception
'PDOException' with message 'invalid
data source name' in
E:\PortableApps\xampp\htdocs\dbcon.php:24
Stack trace: #0
E:\PortableApps\xampp\htdocs\dbcon.php(24):
PDO->__construct('', NULL, NULL) #1
E:\PortableApps\xampp\htdocs\login.php(4):
user->dbConnect() #2 {main} thrown in
E:\PortableApps\xampp\htdocs\dbcon.php
on line 24

Can anybody see what I'm doing wrong, as I'm sure it has to do with my lack of knowledge when it comes to classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-09-10 13:59:49
$this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);

当您访问类实例的变量时,您必须使用 ->;操作员。在这种情况下,您可以使用 $this->dbinfo 而不是 $dbinfo 并使用 $this->con 而不是 >$con。你在左边做得正确,但在右边却错过了一些。

$this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);

When you acces variables of a class instance you have to use the -> operator. In this case you'd use $this->dbinfo instead of just $dbinfo and $this->con instead of $con. You've done it correctly on the left side, but missed some on the right.

如何视而不见 2024-09-10 13:59:49

不要在连接字符串中添加空格。 ("; dbname=" 应该是 ";dbname=")。

另外,在某些情况下,您需要在某些类实例变量前面添加 $this->$this->con>$this->dbinfo 等等)。

Don't put spaces in your connection string. ("; dbname=" should be ";dbname=").

Also, there are a couple of instances where you need to add a $this-> in front of some class instance variables ($this->con, $this->dbinfo et cetera).

绝對不後悔。 2024-09-10 13:59:49

这样做:$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
Int this: $this->dbcon = new PDO($this->con, $dbinfo['user'], $dbinfo['password']);

Make this: $this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
Int this: $this->dbcon = new PDO($this->con, $dbinfo['user'], $dbinfo['password']);

予囚 2024-09-10 13:59:49

这是 dsn 语法:https://www.php。 net/manual/en/ref.pdo-mysql.connection.php
我没有看到其中有任何空格,也许你应该删除分号后添加的空格......

Here is the dsn syntax : https://www.php.net/manual/en/ref.pdo-mysql.connection.php
I don't see any space in it, maybe you should remove the one you added after the semicolon...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文