我有以下代码,用于从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/examplep/www/oop/editProperty.php,以及在/opt/lamp/htdocs/examplep/www/oop/classes/dbClass.php中在线定义22
注意:未定义的变量:中的rowSet/第26行的opt/lampp/htdocs/examplep/www/oop/classes/dbClass.php
警告:mysql_fetch_assoc()要求参数1为resource,null在/opt/lamp/htdocs/examplep/www/oop/classes/dbClass.php中给出26
非常感谢您的帮助。感谢
您将方法定义为
public function processRowSet($rowSet, $singleRow=false){
^^^^^^----required
^^^^^^---optional
然后称之为
$results = $db->processRowSet();
^---no arguments at all
如果您真的READ错误消息,您就会意识到这一点。
所有的数据库代码也只是假设世界是完美的,没有什么会失败。您的错误消息清楚地表明某些东西确实失败了。由于你没有错误检查,你只是错误地使用了坏数据。