简体   繁体   English

从mysql数据库中获取所有记录

[英]Get all records from mysql Database

I've been trying to do output all of the data from the table but when I do it, it outputs only the first one.我一直在尝试从表中输出所有数据,但是当我这样做时,它只输出第一个。 I want it to output in my flash application using amfphp.我希望它使用 amfphp 在我的 Flash 应用程序中输出。 But it only outputs the first line :但它只输出第一行:

if (mysqli_num_rows($result) > 0) {

    $sql = "SELECT * FROM chatStorage";
    $result = mysqli_query($conn, $sql);
    $able = true;

    if (mysqli_num_rows($result) > 0) {
        // output data of each row

        while($row = mysqli_fetch_array($result)) {

                return $row["user"].": ".$row["chatText"];

        }
    }
}

You need to display all values , use print or echo :您需要显示所有值,使用 print 或 echo :

$sql = "SELECT * FROM chatStorage";
$result = mysqli_query($conn, $sql);
$able = true;

if (mysqli_num_rows($result) > 0) {
  // output data of each row

  while($row = mysqli_fetch_array($result)) {

    print $row["user"].": ".$row["chatText"].PHP_EOL;

  }
}

Or if you want to return an array indexed :或者,如果您想返回一个索引数组:

$sql = "SELECT * FROM chatStorage";
$result = mysqli_query($conn, $sql);
$able = true;
$myarray = array();

if (mysqli_num_rows($result) > 0) {
  // output data of each row

  while($row = mysqli_fetch_array($result)) {

    $myarray[$row["user"]][] = $row["chatText"];

  }
}
return $myarray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM