我的数据库中有2行数据,我正在尝试获取所有行
$query = mysqli_query($connection, "select username, email, is_admin from adminUsers");
$results = mysqli_fetch_assoc($query);
我已经尝试了mysqli_fetch_assoc,mysqli_fetch_row,mysqli_fetch_array,所有这些都只返回1行数据。
答案 0 :(得分:3)
mysqli_fetch_*()
之外的 mysqli_fetch_all()
个函数,如果支持,则获取一行。循环和获取:
while($row = mysqli_fetch_assoc($query)) {
print_r($row);
//or you can echo $row['username'] etc...
//or store in an array to loop through later
$rows[] = $row;
}
如果您使用mysqlnd
,则有mysqli_fetch_all()
或使用:
if(!function_exists('mysqli_fetch_all')) {
function mysqli_fetch_all($result, $resulttype=MYSQLI_BOTH) {
while($row = mysqli_fetch_array($result, $resulttype)) {
$rows[] =$row;
}
return $rows;
}
}
$results = mysqli_fetch_all($query);
但是你必须遍历所有返回的行:
foreach($results as $row) {
print_r($row);
//or you can echo $row['username'] etc...
}
答案 1 :(得分:2)
PDOStatement::fetchAll
<?php
//.. Do the neccessary PDO connections...
$sth = $dbh->prepare("select username, email, is_admin from adminUsers");
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
<强>(或)强>
$query = mysqli_query($connection, "select username, email, is_admin from adminUsers");
$res = array();
while($results = mysqli_fetch_assoc($query))
{
$res[] = $results;
}
print_r($res);
答案 2 :(得分:1)
使用while
循环获取所有行:
while ($result = mysqli_fetch_assoc($query))
{
// Rest of your code. See an example below
echo $result['username'];
}