1

我正在检索我的查询行:

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

我能怎么做:

(PSEUDO CODE)
for each row in rows
  echo row

我可以只“回显 $rows”,但这不允许我在单独的段落中浏览每一行。

作为第二个问题,我怎样才能遍历一行的每一列:

(PSEUDO CODE)
for each row in rows
  for each column in row
    echo column
4

4 回答 4

4

你的伪代码很好..你只需要把它变成PHP....

回答第一个问题:

// Loop through each row and print it
foreach( $rows as $row ):
   print_r( $row )
endforeach;

第二个问题..类似于:

foreach( $rows as $row ):
   foreach( $row as $col ):
      echo $col;
   endforeach;    
endforeach;
于 2013-08-12T12:23:24.847 回答
4

我会使用这样的东西:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo $row['name_of_your_column'];
}

结果是:

$result = mysqli_query($connection, $query);
于 2013-08-12T12:27:48.490 回答
1

我写了一个小代码,希望它能解决查询。

    //Query to be executed;
$qry="select * from `tableName`";
//Performs a query on the database;
//here conn is a link identifier returned by mysqli_connect or mysqli_init function;
$result=mysqli_query($conn,$qry);
//Gets the number of rows in a result;
$rowcount=mysqli_num_rows($result);
//Fetches all result rows as an associative array;
$row = mysqli_fetch_all($result,MYSQLI_ASSOC);
//Iterating each row;
for($i=0;$i<$rowcount;$i++)
{
    echo "<br> ".$row[$i]['`column_name_1`']." ".$row[$i]['`column_name_2`']." ".$row[$i]['`column_name_3`'];
}

示例 数据库表 Mysql 表快照 代码

//here conn is a link identifier returned by mysqli_connect or mysqli_init function;
$conn = mysqli_connect("localhost","root","","nsgdb") or die('Error connecting to MySQL server.');
//Query to be executed;
$qry="select * from users";
//Performs a query on the database;
$result=mysqli_query($conn,$qry);
//Gets the number of rows in a result;
$rowcount=mysqli_num_rows($result);
//Fetches all result rows as an associative array;
$row = mysqli_fetch_all($result,MYSQLI_ASSOC);
//Iterating each row;
for($i=0;$i<$rowcount;$i++)
{
   echo "<br> ".$row[$i]['id']." ".$row[$i]['user']." ".$row[$i]['pass'];
}

代码的输出

于 2017-03-17T06:02:25.720 回答
0
$rows = mysqli_fetch_assoc($result, MYSQLI_ASSOC);

使用该mysqli_fetch_assoc功能。

并将 foreach 用作:

foreach($rows as $column => $value) {
  echo $column." = ".$value;
}

注意:您也可以使用foreachwith mysqli_fetch_all

于 2013-08-12T12:25:12.993 回答