doushaizhen1244 2017-11-08 11:01
浏览 33
已采纳

在PHP中返回带有mysql查询的字符串列表

I'm trying to make a simple query with PHP that returns a list of Strings with all the names of the users, but I can figure out how to do do it. The only thing i thought wa this:

<?php

$host_name  = "hostname";
$database   = "database";
$user_name  = "username";
$password   = "pass";

$connect = mysqli_connect($host_name, $user_name, $password, $database);

$select = "SELECT username from userstasker";

$result = $connect->query($select);
$rows = array();

while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}

echo json_encode($rows);

$connect->close();
?>

But this returns a JSon, ant idea how to do it?

  • 写回答

1条回答 默认 最新

  • douhua9726 2017-11-08 11:19
    关注
    <?php
    
    $host_name  = "hostname";
    $database   = "database";
    $user_name  = "username";
    $password   = "pass";
    
    $connect = mysqli_connect($host_name, $user_name, $password, $database);
    
    $select = "SELECT username from userstasker";
    
    $result = $connect->query($select);
    $usernames = array();
    
    while($r = mysqli_fetch_assoc($result)) {
        $usernames[] = $r["username"];
    }
    
    $connect->close();
    
    // Do something with $usernames
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?