oop中出现了什么问题?

时间:2013-08-20 19:04:29

标签: php oop

我有以下代码,用于从users表中选择所有内容。

<?php

class DB{

    protected $db_name = 'oop';
    protected $db_user = 'root';
    protected $db_pass = '';
    protected $db_host = 'localhost';

    //Open a connection to the database. Make sure this is called
    //on evey page that needs to use the database. 
    public function connect(){
        $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name);

        return true;
    }

    //Takes mysql row set and returns and associative array, where the keys
    //in the array are the column names in the row set. If singleRow is set to
    //true, then it will return a single row instead of an array of rows. 
    public function processRowSet($rowSet, $singleRow=false){

        $resultsArray = array();

        while($row = mysql_fetch_assoc($rowSet)){
            array_push($resultsArray, $row);
        }

        if($singleRow === true){
            return $resultsArray[0];
        }

        return $resultsArray;
    }

    //Select rows from the database. 
    //Returns a full row or rows from $table using $where as the where clause. 
    //Return value is an associative array with column names as keys. 
    public function select($table, $where){

        $sql = "SELECT * FROM $table";

        $result = mysql_query($sql);

        if(mysql_num_rows($result) == 1){

            return $this->processRowSet($result, true);
        }


        return $this->processRowSet($result);
    }

    //Updates a current row in the database. 
    //Takes an array of data, where the keys in the array are the columns names
    //and the values are the data that will be inserted into those columns. 
    //$table is the name of the table and $where is the sql where clause. 
    public function update($data, $table, $where){

        foreach ($data as $column => $value){
            $sql = "UPDATE $table SET $column = $value WHERE $where";
            mysql_query($sql) or die (mysql_error());
        }

        return true;
    }

    //Inserts a new row into the database. 
    //Takes an array of data, where the keys in the array are the column names
    //and the values are the data that will be inserted into those columns. 
    //$table is the name of the table
    public function insert($data, $table) {
        $columns = "";
        $values = "";

        foreach ($data as $column => $value) {
            $columns .= ($columns == "") ? "" : ", ";
            $columns .= $column;
            $values .= ($values == "") ? "" : ", ";
            $values .= $value;
        }

        $sql = "INSERT INTO $table ($columns) VALUES ($values)";
        mysql_query($sql) or die(mysql_error());

        //return the ID of the user in the database.
        return mysql_insert_id();
    }
}    

?>

我试着这样称呼它:

$db = new DB();
$db->connect();
$db->select('users', '');
$results = $db->processRowSet();
print_r($results);

由于我不断遇到错误,我做错了什么:

  

警告:在调用DB :: processRowSet()时缺少参数1   第15行的/opt/lampp/htdocs/xampp/www/oop/editProperty.php和   在/opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php中定义在线   22

     

注意:未定义的变量:rowSet in   第26行/opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php

     

警告:mysql_fetch_assoc()期望参数1为resource,null   在/opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php中给出   26

非常感谢您的帮助。感谢

1 个答案:

答案 0 :(得分:2)

您将方法定义为

public function processRowSet($rowSet, $singleRow=false){
                               ^^^^^^----required
                                         ^^^^^^---optional

然后将其称为

$results = $db->processRowSet();
                              ^---no arguments at all 

如果您确实阅读错误消息,那么您已经意识到这一点。

您的所有数据库代码也只是假设世界是完美的,没有任何东西会失败。您的错误消息清楚地表明DID失败。由于您没有错误检查,因此您只是使用错误数据进行错误处理。