如何在php中所有行中回显列的所有名称

时间:2022-09-23 21:16:02

I have a remote mysql database and a table with several rows. I want to echo one specific column (attribute) name in all rows.

我有一个远程mysql数据库和一个有几行的表。我想在所有行中回显一个特定的列(属性)名。

Below is the php code:

下面是php代码:

<?php
  require "conn.php";
  $command = $_POST['command'];
  $mysql_qry = "select name from college_data;";
  $result = mysqli_query($conn, $mysql_qry);
  $result_details=mysqli_fetch_assoc($result);

  if(mysqli_num_rows($result) > 0) {
     echo $result_details[0].$result_details[1]." Listall successfully!"; // how to change this line of code
}
  else {
      echo " Listall fails!";
  }
?>

More specifically, how to display all the data stored in result_details?

更具体地说,如何显示result_details中存储的所有数据?

2 个解决方案

#1


2  

Your code is a mess

你的代码一团糟

if(mysqli_num_rows($result) > 0) {
     echo $result_details[0].$result_details[1]." Listall successfully!"; // how to change this line of code
}

You're fetching only one column. And you're not looping over the result set. Finally, you're fetching an associative array but using numeric keys

只取一个列。最后,获取一个关联数组,但是使用数字键

So let's clean this up

我们来清理一下。

while($result_details = mysqli_fetch_assoc($result)) {
     echo $result_details['name'] . '<br>';
}

So now we're iterating over your full result set. We're using associative keys as well.

现在我们在遍历整个结果集,我们也在使用关联键。

#2


0  

Change the code inside the if statement to:

将if语句中的代码更改为:

foreach($result_details as $row){
  if(is_array($row)){
    foreach($row as $row2){
      echo $row2 . '<br/>';
    }
  }else{
    echo $row . '<br/>';
  } 
} 

#1


2  

Your code is a mess

你的代码一团糟

if(mysqli_num_rows($result) > 0) {
     echo $result_details[0].$result_details[1]." Listall successfully!"; // how to change this line of code
}

You're fetching only one column. And you're not looping over the result set. Finally, you're fetching an associative array but using numeric keys

只取一个列。最后,获取一个关联数组,但是使用数字键

So let's clean this up

我们来清理一下。

while($result_details = mysqli_fetch_assoc($result)) {
     echo $result_details['name'] . '<br>';
}

So now we're iterating over your full result set. We're using associative keys as well.

现在我们在遍历整个结果集,我们也在使用关联键。

#2


0  

Change the code inside the if statement to:

将if语句中的代码更改为:

foreach($result_details as $row){
  if(is_array($row)){
    foreach($row as $row2){
      echo $row2 . '<br/>';
    }
  }else{
    echo $row . '<br/>';
  } 
}