-5

我有以下代码,它旨在从用户表中选择所有内容。

<?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/ 中定义第 22 行的 classes/dbClass.php

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

警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 26 行的 /opt/lampp/htdocs/xampp/www/oop/classes/dbClass.php 中给出 null

您的帮助将不胜感激。谢谢

4

1 回答 1

2

您将方法定义为

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

然后将其称为

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

如果你真的阅读了错误信息,你就会意识到这一点。

您所有的数据库代码也只是假设世界是完美的,没有任何事情会失败。您的错误消息清楚地表明某些事情确实失败了。由于您没有错误检查,因此您只是使用不良数据犯了错误。

于 2013-08-20T19:08:36.263 回答