【问题标题】:I created the code using mysqli_fetch_assoc with 'while' , but it's not woking我使用带有 'while' 的 mysqli_fetch_assoc 创建了代码,但它不起作用
【发布时间】:2019-03-04 01:53:15
【问题描述】:

我使用 mysqli_fetch_assoc 和 'while' 创建了代码,如下所示。 但它不起作用。

if ($result = mysqli_query ($dbconn, $query)) { 它在这里工作。

while ($row = mysqli_fetch_assoc($result)) { 从这里它不起作用。

我找不到错误的部分。 如果我不使用'while',它的工作原理如下。

有什么问题?

// not works
$query = "select * from member where f_status='1'";

if ($result=mysqli_query($dbconn, $query)) {
	while ($row = mysqli_fetch_assoc($result)) {
		if ($row[f_status]==0) {
			error("No data");
		} else {
			echo $row[f_user_id];
			echo $row[f_user_name];
		}
	}
	mysqli_free_result($result);
}



// works
$query = "select * from member where f_status='1'";
$result = mysqli_query($dbconn, $query);
$row = mysqli_fetch_assoc($result);

if ($row) {
	echo $row[f_user_id];
	echo $row[f_user_name];
} else {
	error("No data");
}

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    我认为问题在于您迭代 while 循环和条件以显示 No data 消息的方式,

    你应该试试这个方法:

    $query = "select * from member where f_status='1'";
    
    if ($result = mysqli_query($dbconn, $query)) {
        if ($result->num_rows) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo $row[f_user_id];
                echo $row[f_user_name];
            }
        }
        else {
            error("No data");
        }
        mysqli_free_result($result);
    }
    

    希望这能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2023-04-08
      • 2023-01-19
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      相关资源
      最近更新 更多