【问题标题】:is that nested methods right? why I get empty query?那是嵌套方法吗?为什么我得到空查询?
【发布时间】:2023-04-02 10:55:01
【问题描述】:

我想知道为什么我的代码会出错?有人可以帮忙吗? 我的课是使用嵌套方法从数据库中获取一些信息,假设我得到一个空查询

<?php
class db {
 public function __construct(){
 if(mysql_connect("localhost","root","0000")){
     mysql_select_db("myblog");

 }else{
     echo mysql_error();
 }

 }

   public function select($row){
      $sql="SELECT".$row;
      return $this;
   }

   public function from($table){
      $sql.="FROM".$table;
      return $this;
   }

   public function where($condition){
     $sql.="WHERE".$condition;
      return $this;
   }

}
$ddb=new db;
$qq=$ddb->select("*")->from("news")->where("id='1'");
$query=  mysql_query($qq);
while($row=mysql_fetch_object($query)){
    echo $row->title;
}
?>

【问题讨论】:

  • 为您的每个方法添加调试,以便您了解它们返回的内容。回显完成的语句,以便您查看它的样子。
  • 您的查询最终没有空格...SELECT*FROMnewsWHEREid=1
  • @rsplak,实际上,mysql_query() 在这段代码中甚至没有得到字符串。
  • 当放置一个 echo 语句时,输出是 SELECT *SELECT *FROM newsSELECT *FROM newsWHERE id=1 我怎样才能解决这个问题并获得正确的查询?

标签: php mysql methods nested


【解决方案1】:

您必须定义 __toString() 特殊方法才能将您的对象用作字符串:

class db {

    private $sql = '';

    public function __construct() {
        if (mysql_connect("localhost", "root", "0000")) {
            mysql_select_db("myblog");
        } else {
            echo mysql_error();
        }
    }

    public function select($row) {
        $this->sql = "SELECT ".$row;
        return $this;
    }

    public function from($table) {
        $this->sql .= " FROM ".$table;
        return $this;
    }

    public function where($condition) {
        $this->sql .= " WHERE ".$condition;
        return $this;
    }

    public function __toString() {
        return $this->sql;
    }

}

$ddb = new db();
$qq = $ddb->select("*")->from("news")->where("id='1'");
$query = mysql_query($qq);
while ($row = mysql_fetch_object($query)) {
    echo $row->title;
}

【讨论】:

    【解决方案2】:

    您需要在每个方法中使用$this 才能附加到$sql

       // Declare the class property
       public $sql = "";
    
    
       // And use $this->sql in the methods
       // Also note whitespace added around SELECT, FROM, WHERE
       public function select($row){
          $this->sql="SELECT ".$row;
          return $this;
       }
    
       public function from($table){
          $this->sql.=" FROM ".$table;
          return $this;
       }
    
       public function where($condition){
         $this->sql.=" WHERE ".$condition;
          return $this;
       }
    

    然后当你查询它时,使用$ddb-&gt;sql,因为你没有返回 SQL 字符串。

    $ddb->select("*")->from("news")->where("id='1'");
    $query = mysql_query($ddb->sql);
    

    不言而喻,希望您打算在构造到 where() 方法中的任何变量上调用 mysql_real_escape_string()。事实上,你没有特别的逃避它。

    【讨论】:

      【解决方案3】:

      在 $qq 上运行 var_dump

      这将立即向您显示问题。

      在我看来,您的查询中似乎缺少几个空格,并且您没有返回查询字符串 $sql 而是在您的数据库类中返回 $this

      【讨论】:

      • 这对我检测错误很有用,但没有解决所有问题,谢谢
      【解决方案4】:
      • 在方法selectfromwhere 中使用$this-&gt;sql 而不是$sql
      • 像这样输出查询:$ddb-&gt;select("*")-&gt;from("news")-&gt;where("id='1'")-&gt;sql 或创建一个 getSQL() 方法,它只返回 $sql 字段并像 $ddb-&gt;select("*")-&gt;from("news")-&gt;where("id='1'")-&gt;getSQL() 一样查询它

      您还应该考虑创建一个方法来创建mysql_query(不仅仅是字符串)或一些用于检索方法的方法,这样更容易使用。

      【讨论】:

        猜你喜欢
        • 2017-01-11
        • 2021-07-03
        • 2017-10-01
        • 1970-01-01
        • 2019-11-13
        • 1970-01-01
        • 2020-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多