【问题标题】:Connect to DB and Activites by OOP in PHP通过 PHP 中的 OOP 连接到数据库和活动
【发布时间】:2014-02-14 22:46:31
【问题描述】:

您好,我在使用 OO PHP 连接到我的数据库时遇到了一些问题。我的脚本如下。我已经有一段时间了。这只是一个测试脚本,因为我对 OOP 还很陌生。请不要苛刻

class Database{

    public $mysqli,
         $host,
         $username,
         $password,
         $db;

    public function __construct($host, $username, $password, $db){

      $this->host = $host;
      $this->username = $username;
      $this->password = $password;
      $this->db = $db;

     $mysqli = new mysqli($host, $username, $password, $db);
        if (!$mysqli){
            echo "error in connecting to database";
        }
        else{
            echo "success in connecting to database";
        }
    }   

    public function query(){
        $result = $mysqli->query("SELECT * FROM inventory");
        if ($result) {
            printf("Select returned %d rows.\n", $result->num_rows);

            $result->close();
        }
        else{
            echo "there is an error in query";
            $result->close();
        }
        //echo "in query function";
    }
}

用法...

$DB = new Database('localhost', 'root', 'xxxx', 'yyyy');
$DB -> query();  

【问题讨论】:

  • 如果您“遇到一些问题”,您需要在问题中添加所有可用信息。 描述问题和症状否则很难诊断。

标签: php oop mysqli this


【解决方案1】:

问题是,您正在访问没有$this 的类属性。 $mysqli 应该在构造函数中的$this->mysqli 中。

您正在使用 mysqli 属性作为 mysqli classObject

那么就可以在查询函数中使用$this->mysqli->query("SELECT * FROM inventory")mysqli属性对象(In Database class)的query函数了。

更新代码:

<?php
class Database{

    public $mysqli,
         $host,
         $username,
         $password,
         $db;

public function __construct($host, $username, $password, $db){

  $this->host = $host;
  $this->username = $username;
  $this->password = $password;
  $this->db = $db;

 $this->mysqli = new mysqli($host, $username, $password, $db);

    /* check connection */
    if ($this->mysqli->connect_errno) {
       printf("Connect failed: %s\n", $this->mysqli->connect_error);
       exit();
    } else {
       echo "success in connecting to database";
    }
}   

public function query(){
    $result = $this->mysqli->query("SELECT * FROM inventory");
    if ($result) {
        printf("Select returned %d rows.\n", $result->num_rows);

        $result->close();
    }
    else{
        echo "there is an error in query";
        $result->close();
    }
    //echo "in query function";
}
}

$DB = new Database('localhost', 'root', 'xxxx', 'yyyy');
$DB -> query();  

【讨论】:

  • 失败的mysqli 构造函数不会评估为 falsey 值。你需要检查mysqli::$connect_error/mysqli::$connect_errno的值
【解决方案2】:

您的主要问题是您没有将值存储到您的类的$mysqli 属性中。您需要在构造函数和查询方法中使用$this-&gt;mysqli 而不是$mysqli

其次,这个类添加了mysqli 类没有的nothing。您不妨简单地使用

$DB = new mysqli('localhost', 'root', 'xxxx', 'yyyy');

if ($DB->connect_error) {
    throw new Exception($DB->connect_error, $DB->connect_errno);
}

【讨论】:

  • pdo和mysqli哪个更好>
  • @user2128444 这完全是主观的。我认为 PDO 有一个更简洁的 API,但是有一些 MySQL 功能只能通过 MySQLi 使用,所以你可以根据你将如何使用连接来选择。
【解决方案3】:

试试这个:

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

// Test if connection succeeded
if(mysqli_connect_error()){
    die("Database connection failed: " .
        mysqli_connect_error() .
        " (" . mysqli_connect_errno() . ")"
    );
}

【讨论】:

  • 这不是程序吗?
猜你喜欢
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2016-08-10
相关资源
最近更新 更多