[英]What does “while($row = mysqli_fetch_assoc($result))” do?
[英]What is the wrong with “while($row = mysqli_fetch_assoc($result)){// }”?
我尝试了几种方式的以下代码。 但是问题总是出现在"$row = mysqli_fetch_assoc($result)"
..... print_r()
方法将结果打印为数组,这意味着MYSQL查询没有错误。 它给出正确的结果。 问题出在assoc中。 而不是'assoc'
我也使用了'array'
。
错误说:“警告:mysqli_fetch_assoc()期望参数1为mysqli_result,第21行的C:\\ xampp \\ htdocs \\ offoptimizer2 \\ action \\ fetch_client.php中给出的数组”
class fetch_client{
public function fetchdata(){
$phn_number = $_GET["phn"];
$db_action = new db_action();
$result = $db_action->select_all("offopt_client","client_phn_no='".$phn_number."'");
print_r($result);
echo '<table border="0">';
echo '<tr>';
echo '<th> client name </th>';
echo '<th> client phone number </th>';
echo '</tr>';
if($result != null){
while($row = mysqli_fetch_assoc($result)){
echo '<tr>';
echo "<td> ".$row['client_name']." </td>";
echo "<td> ".$row['client_phn_no']." </td>";
echo '</tr>';
}
echo '</table>';
}else{
echo 'No result';
}
}
}
//这是我下面的select_all函数
public function select_all($table_name, $where){
$db_connect = new db_connect();
$con = $db_connect->connect();
if(!$con){
echo "Connection error";
}else{
$query = mysqli_query($con,"select * from ".$table_name." where ".$where." ");
if(!$query){
echo "Error in your query".mysqli_error($query);
}else{
$result = mysqli_fetch_array($query);
return $result;
}
}
}
如下更新您的“ select_all”功能。
public function select_all($table_name, $where){
$db_connect = new db_connect();
$con = $db_connect->connect();
if(!$con){
echo "Connection error";
}else{
$query = mysqli_query($con,"select * from ".$table_name." where ".$where." ");
if(!$query){
echo "Error in your query".mysqli_error($query);
}else{
$result = array();
while($row = mysqli_fetch_assoc($query))
array_push($result, $row);
return $result;
}
}
}
现在,您可以对以下结果使用foreach
循环:
foreach($result as $row)
echo '<tr>';
echo "<td> ".$row['client_name']." </td>";
echo "<td> ".$row['client_phn_no']." </td>";
echo '</tr>';
}
mysqli_fetch_assoc
需要一个ressource
而不是一个array
。 因此,这看起来像您的$db_action->select_all
方法将您的数据库数据返回到数组中一样,因此对mysqli
函数执行附加任务并产生结果是行不通的。
如果您希望获取assoc返回数据,请在$db_action
之后编辑您的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.