PHP 与PDO:可以连接DB;好像无法查询

发布于 2024-10-05 05:57:40 字数 948 浏览 3 评论 0原文

看来我可以使用 PDO 连接到我的数据库,但无法用它执行任何查询。示例:

 private function connect() {
      try {
           $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass);
      }
           catch (PDOException $e) {
           die ($e);
      }
      print_r($link);
      $result = $link->query("select * from mt3_users");
      var_dump($result);
      $row = $result->fetch($result);
      die("Your id is: ".$row["id"]);
      //$link = mysql_connect($this->sHost, $this->sUser, $this->sPass);
      if (!$link) {
          echo "Failed to connect to $this->sHost!";
          return false;
      }
      return $link;
 }

这将返回以下内容:

PDO 对象 ( ) bool(false) 致命错误:在 Database.php 第 32 行的非对象上调用成员函数 fetch()

所以基本上,$link 作为 PDO 对象返回(我更改了用户名和密码以查看是否捕获了异常;是的)并且 PDOConnection::Query 由于某种原因返回 null。这是我第一次处理 PDO——我是在做一些有趣的事情吗?

It appears I can connect to my database using PDO, but can't execute any queries with it. Example:

 private function connect() {
      try {
           $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass);
      }
           catch (PDOException $e) {
           die ($e);
      }
      print_r($link);
      $result = $link->query("select * from mt3_users");
      var_dump($result);
      $row = $result->fetch($result);
      die("Your id is: ".$row["id"]);
      //$link = mysql_connect($this->sHost, $this->sUser, $this->sPass);
      if (!$link) {
          echo "Failed to connect to $this->sHost!";
          return false;
      }
      return $link;
 }

This returns the following:

PDO Object ( ) bool(false)
Fatal error: Call to a member function fetch() on a non-object in Database.php on line 32

So basically, $link is coming back as a PDO object (I changed my username and password to see if an exception was caught; it was) and PDOConnection::Query is returning null for some reason. This is my first time dealing with PDOs -- am I doing something funny?

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

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

发布评论

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

评论(4

乱了心跳 2024-10-12 05:57:40

查询很可能会失败,您确定表 mt3_users 的名称以及您是否选择了正确的数据库?该错误消息显示 $result 不是对象,这是由于查询中的错误造成的。

另外:

$row = $result->fetch($result);

应该是,

$row = $result->fetch();

除非您想为 fetch() 指定选项,但不将对象作为参数传递。

Most likely the query fails, are you sure of the name of the table mt3_users and that you have selected the right database? That error message shows that $result is not an object and that's due to an error in the query.

Also:

$row = $result->fetch($result);

should be

$row = $result->fetch();

unless you want to specify options to fetch(), but you don't pass the object as argument.

遗心遗梦遗幸福 2024-10-12 05:57:40

Array ( [0] => 00000 [1] => 1046 [2] => 未选择数据库 ) Array ( )

没关系,我想。事实证明,在从使用常规 MySQL 函数迁移时,我没有设置 $this->sName ($this->sName 为 null。我很惊讶它没有抛出异常。不过,只有一半)

固定的。

不过还是谢谢你们的回答:)

Array ( [0] => 00000 [1] => 1046 [2] => No database selected ) Array ( )

Nevermind, I guess. It turns out that while migrating from using regular MySQL functions, I wasn't setting $this->sName ($this->sName was null. I'm half surprised it didn't throw an exception. Only half, though)

Fixed.

But thank you guys for the answers :)

请帮我爱他 2024-10-12 05:57:40

您可以尝试一下吗

 private function connect() {
 try {
   $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass); 
 return $link ;
 }
 catch (PDOException $e) {
 die ($e);
 }
}
$pdolink = $this->connect(); 
$rows = $pdolink->query("select * from mt3_users");
foreach($rows as $row ){
   echo("Your id is: ".$row["id"]);
 } 

?如果您需要坚持使用 fetchAll 函数

  private function connect() {
     try {
       $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass); 
     return $link ;
     }
     catch (PDOException $e) {
     die ($e);
     }
    }
    $pdolink = $this->connect(); 
    $q = $pdolink->prepare("select * from mt3_users");
    $q->exectue();
    $rows = $q->fetchAll();
    var_dump($rows);
    foreach($rows as $row ){
       echo("Your id is: ".$row["id"]);
     } 

can you give this a try

 private function connect() {
 try {
   $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass); 
 return $link ;
 }
 catch (PDOException $e) {
 die ($e);
 }
}
$pdolink = $this->connect(); 
$rows = $pdolink->query("select * from mt3_users");
foreach($rows as $row ){
   echo("Your id is: ".$row["id"]);
 } 

and if you need to stick with fetchAll function

  private function connect() {
     try {
       $link = new PDO("mysql:host=$this->sHost;dbname=$this->sName", $this->sUser, $this->sPass); 
     return $link ;
     }
     catch (PDOException $e) {
     die ($e);
     }
    }
    $pdolink = $this->connect(); 
    $q = $pdolink->prepare("select * from mt3_users");
    $q->exectue();
    $rows = $q->fetchAll();
    var_dump($rows);
    foreach($rows as $row ){
       echo("Your id is: ".$row["id"]);
     } 
断肠人 2024-10-12 05:57:40

为了得到查询中的错误:

$result = $link->query(...);
if($result===FALSE){
   print_r( $link->errorInfo);
   exit();
}

// the correct way to fetch one row
$link->fetch(PDO::FETCH_ASSOC); // or whatever way you want to fetch data

In order to get the error in your query:

$result = $link->query(...);
if($result===FALSE){
   print_r( $link->errorInfo);
   exit();
}

// the correct way to fetch one row
$link->fetch(PDO::FETCH_ASSOC); // or whatever way you want to fetch data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文