-1

由于我是 php 新手,我不确定可能是什么问题:

类.ManageUsers.php

<?php


include_once('class.database.php');
class ManageUsers{

    public $link;
    function _construct(){
        $db_connection =  new dbConnection();
        $this->link = $db_connection->connect();
        return $this->link;
    }
    function registerUsers($username, $password, $ip_address, $time, $date){
        $query = $this->link->prepare("INSERT INTO TESTUSERS(username, password, ip_address, reg_time, reg_date ) values (?,?,?,?,?)");
        $values = array($username, $password, $ip_address, $time, $date);
        $query->execute($values);
        $counts = $query->rowCount();
        return $counts;
    }

}
$user = new ManageUsers();
echo $user->registerUsers('foo', 'foo','127.0.0.1','12:00','12-9-2013');
    ?>

类.database.php

    <?php
    class dbConnection{
protected $db_conn;
public $db_name ='applicationdb';
public $db_user = 'root';
public $db_password = '';
public $db_host = 'localhost';
function connect(){
$this->db_conn = new PDO("mysql: host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_password);
return $this->db_conn;
}
    }
 ?>
4

3 回答 3

3

__construct 方法中你没有忘记另一个下划线吗?

于 2013-09-14T19:25:57.827 回答
2

您的构造函数没有被调用,因为正确的签名是 __construct()

于 2013-09-14T19:27:05.387 回答
0

该错误说明了它的含义:

$this->link

不是一个对象,在

query = $this->link->prepare( ... );

因为

$this->link = $db_connection->connect();

永远不会执行,因为错字( __construct 带有一个下划线而不是两个。)

于 2013-09-14T19:34:16.223 回答