OOP PHP数据库结果到Array

时间:2016-10-28 10:56:22

标签: php oop

我对oop php的想法很新,我正在尝试编写一个irc php。

我想做的事情: 我正在尝试查询我的数据库,从我的数据库中获取结果并将其放入我的程序中的数组中。

我尝试创建一个新函数来执行任务,并在__construct函数中调用它。

我缩短了代码,但它看起来很像:

非常感谢任何想法和想法。

class IRCBot
{    

    public $array = array();
    public $servername = "localhost";
    public $username = "root";
    public $password = "usbw";
    public $dbname = "bot";


function __construct()
{
    //create new instance of mysql connection
    $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 

    if ($mysqli->connect_errno) 
    {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    echo $mysqli->host_info . "\n";

    $this->database_fetch();

}

function database_fetch()
{
    $query = "SELECT word FROM timeoutwords";
    $result = mysqli_query($query);
    while($row = mysqli_fetch_assoc($result))
        {
            $array[] = $row();
        }
}

function main()
{
    print_r($array);
}



}

$bot = new IRCBot();

2 个答案:

答案 0 :(得分:3)

首先,您需要修改构造函数中的错误,您可以修改为:

function __construct()
{
    //create new instance of mysql connection
    $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 

    if ($this->conn->connect_errno) 
    {
        echo "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
    }
    echo $this->conn->host_info . "\n";
}
  • 在此处,您需要将$mysqli替换为$conn,因为您的链接标识符为$conn而不是$mysqli
  • 无需在此致电database_fetch()
  • 您需要使用$conn作为财产。

现在您需要将database_fetch()方法修改为:

function database_fetch()
{
    $query = "SELECT word FROM timeoutwords";
    $result = mysqli_query($this->conn,$query);
    $array = array();
    while($row = mysqli_fetch_assoc($result))
    {
        $array[] = $row;
    }
    return $array;
}
  • 在这里,您需要在mysqli_query()中传递添加第一个参数,该参数应该是链接标识符/数据库连接。

  • 其次,您需要使用return来获取此函数的结果。

最后,您需要将main()方法修改为:

function main()
{
    $data = $this->database_fetch();
    print_r($data);
}
  • 在此,您需要在此处调用database_fetch()方法,然后在需要的位置打印数据。

答案 1 :(得分:3)

<强> 更改

1)if ($conn->connect_errno)更改为$array[] = $row();

2)$array[] = $row;更改为return $array;

3)在功能database_fetch()

中添加database_fetch()

4)main()函数中调用$this->conn函数而不是构造函数。

5)mysqli_query()中添加<?php class IRCBot { public $array = array(); public $servername = "localhost"; public $username = "root"; public $password = "usbw"; public $dbname = "bot"; public $conn; function __construct() { //create new instance of mysql connection $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); if ($this->conn->connect_errno) { echo "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error; } } function database_fetch() { $query = "SELECT word FROM timeoutwords"; $result = mysqli_query($this->conn,$query); while($row = mysqli_fetch_assoc($result)){ $array[] = $row; } return $array; } function main() { $data = $this->database_fetch(); print_r($data); } } (感谢@devpro指出。)

更新代码

{{1}}

快速入门