PHP数据库类错误

时间:2014-12-26 10:53:09

标签: php mysqli

以下是我使用(尝试)从数据库中提取信息的文件,但它给我一个错误。

配置文件:

define("DBHOST", "127.0.0.1");
define("DBUSER", "root");
define("DBPASS", "root");
define("DBNAME", "test");
define("DBPORT", "3306");

数据库类:

class Database{

    public function __contruct(){
        $con = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME, DBPORT);

        if (!$con){
            die("Error connecting to database");
        }
        else{
            $this->con = $con;
        }
    }

    function SimpleQuery(){
        $query="SELECT * FROM accounts";
        $array = $this->con->query($query);
        while ($fields = $array->fetch_array(MYSQL_ASSOC)){
            $values[] = $fields;
        }
        return $values;
    }
}

索引文件:

require ("config.php");
require ("database.php");

$db = new Database();

echo $rows = $db->SimpleQuery();

给我这个错误:

Fatal error: Call to a member function query() on a non-object in database.php

2 个答案:

答案 0 :(得分:0)

在DB类的con =语句中有2个半冒号

答案 1 :(得分:0)

试试这个:

数据库类:

class Database{

    public function __construct(){
                        // ^ problem here
        $con = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME, DBPORT);

        if (!$con){
            die("Error connecting to database");
        }
        else{
            $this->con = $con;
        }
    }

    function SimpleQuery(){
        $query="SELECT * FROM accounts";
        $array = $this->con->query($query);
        while ($fields = $array->fetch_array(MYSQL_ASSOC)){
            $values[] = $fields;
        }
        return $values;
    }
}

索引文件:

<?php 
//error_reporting(E_ALL);
//ini_set('display_errors',1);
include("submit.php");

$db = new Database();
 $rows = $db->SimpleQuery();
 print_r($rows);