使用PHP OO类获取行
我制作了一个类函数,它将从数据库中的表中获取行,并附带一个可选参数。这项工作时,得到一个单一的行,但我不能得到这项工作时,多行返回 下面是Users类中的内容使用PHP OO类获取行,php,mysql,class,Php,Mysql,Class,我制作了一个类函数,它将从数据库中的表中获取行,并附带一个可选参数。这项工作时,得到一个单一的行,但我不能得到这项工作时,多行返回 下面是Users类中的内容 public function getUsers($filter="") { $Database = new Database(); if($filter == 'male') $extra = "WHERE gender = 'm'"; $sql = "SELECT *
public function getUsers($filter="") {
$Database = new Database();
if($filter == 'male')
$extra = "WHERE gender = 'm'";
$sql = "SELECT *
FROM users
$extra";
if ($Database->query($sql))
return $Database->result->fetch_assoc();
else
return false;
}
数据库类
class Database {
private $db = array();
private $connection;
private $result;
public function __construct() {
$this->connect();
}
public function connect() {
$this->connection = mysqli_connect('mysql.com', 'username', 'pass');
mysqli_select_db($this->connection, 'database');
}
public function query($sql) {
$this->result = mysqli_query($this->connection, $sql);
return $this->result;
}
这是用于尝试和显示行的代码
if ($student = $User->getUsers($filter)) {
echo "<table>\n";
echo "<tr><td>Col 1</td><td>Col 2</td><td>Col 3</td><td>Col 4</td><td></td><td></td></tr>";
foreach($student as $row) {
echo "<tr>";
echo "<td>$row[col1]</td>";
echo "<td>$row[col2]</td>";
echo "<td>$row[col3]</td>";
echo "<td>$row[col4]</td>";
echo "<td>$row[col5]</td>";
echo "<td>$row[col6]</td>";
echo "</tr>\n";
}
echo "</table>";
}
if($student=$User->getUsers($filter)){
回音“\n”;
回声“Col 1Col 2Col 3Col 4”;
foreach($student作为$row){
回声“;
回显“$row[col1]”;
echo“$row[col2]”;
echo“$row[col3]”;
echo“$row[col4]”;
echo“$row[col5]”;
echo“$row[col6]”;
回音“\n”;
}
回声“;
}
(我正在学习OO PHP,请耐心听我说)在您的代码中,
$result
来自哪里并不清楚
您应该调用以下返回的对象的fetch_assoc()方法:
$mysqli->query($query)
为什么要使用
$result->fetch_assoc()
两次?您正在返回一个数组。。您应该处理$student
我仍在学习OOP,但我使用了以下方法:
protected $connection, $result, $_numRows;
public function query($sql)
{
$this->result = mysql_query($sql, $this->connection);
$this->_numRows = mysql_num_rows($this->result);
}
/*
* @desc get result af query
*
* @returns string $result
*/
public function getResult()
{
return $this->result;
}
/*
* @desc Return rows in table
* @returns int $_numRows
*/
public function numRows()
{
return $this->_numRows;
}
/*
* @desc Count rows and add to array
* @return string $rows array
*/
public function rows()
{
$rows =array();
for ($x=0; $x < $this->numRows(); $x++) {
$rows[] = mysql_fetch_assoc($this->result);
}
return $rows;
}
修改最后一部分以满足您的需要。很抱歉,$结果不正确,它不会导致任何结果。我有一个用于查询的数据库类,因此使用$database->query而不是$mysqli。请整理您的代码,也许向我们展示您的数据库类将有助于发现问题……打印($student)的内容是什么?它只返回第一行,而不是所有行。更新以显示数据库类。抱歉,我应该编辑while语句,那里不应该有另一个$result->fetch_assoc()。我尝试过foreach($student as$row),但这不起作用。每次调用fetch_assoc()时,它只返回一行。因此,您必须调用它,直到没有更多的行:
返回一个关联数组,该数组对应于获取的行,如果没有更多的行,则返回NULL。
@Imran我在上面添加了一些代码。我不知道你是否成功了,但是现在代码已经完成了。昨天写这篇文章时我有点累。祝你好运
public function getUsers($filter="") {
$Database = new Database();
if($filter == 'male')
$extra = "WHERE gender = 'm'";
$sql = "SELECT *
FROM users
$extra";
if ($this->numRows() == 0) {
echo "No rows found";
} else {
foreach ($this->rows() as $b) {
$c = array('something' => $b['col']);
}
return $c;
}