douqie6454 2017-04-06 19:55
浏览 87
已采纳

在PHP的数组末尾添加一个数组

I have an 'Android' project where I need to fetch some information from a database. The database is 'MySQL'. I have to go throught a webservice and as I have never done any PHP before now, I hardly see the problem as the logic seems ok to me.

Here is my code:

<?php

        require 'connect.php';

        $username = 'alex@hotma';
        $password = 'soleil';

        $sql = 'SELECT ID, NAME, PASSWORD, EMAIL FROM Account';
        #$sql = 'CALL Login('.$username .', '. $password .')';

        $result = $conn->query($sql);

        $response = array();

        if (!empty($result))
        {
            if($result->num_rows > 0) 
            {
                $response['success'] = 1;
                $users = array();

                while($row = $result->fetch_assoc()) 
                {
                    $user = array();
                    $user["id"] = $row["ID"];
                    $user["name"] = $row["NAME"];
                    $user["password"] = $row["PASSWORD"];
                    $user["email"] = $row["EMAIL"];

                    //Trying to add the user into my users array

                    //If i uncomment this line everything is shown, but not in the response array
                    //echo json_encode($user);
                    array_push($users, $user);
                }
                $response['users'] = $users;
            }
            else
            {
                $response['success'] = 0;
                $response['message'] = 'No user found';
            }
        }
        else
        {
            $response['success'] = 0;
            $response['message'] = 'No user found';
        }

        echo json_encode($response);
        $conn->close();
?>

I currently have more than 6 users in my database, but i cannot seems to get them all. I have left some of my code commented so you can see what i have tried, alas without any success. I want to return a 'JSON' array with all my user inside it.

Do any of you have an idea on how to proceed?

  • 写回答

2条回答 默认 最新

  • duankang5285 2017-04-06 20:47
    关注

    That works IMHO

    <?php
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    include"config.inc.php";
    
    $mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");
    
    if (mysqli_connect_errno()) { echo "Error: " . mysqli_connect_error($mysqli); }
    
    $query = " SELECT ID, NAME, PASSWORD, EMAIL FROM Account ";
    $stmt1 = $mysqli->prepare($query);
    
    $results = $stmt1->execute();
    $stmt1->bind_result($ID, $NAME, $PASSWORD, $EMAIL);
    $stmt1->store_result();
    
      if ($stmt1->num_rows > 0) {
      $users = array();
      $user = array();
      while($stmt1->fetch()){
        echo"[ $ID / $NAME / $PASSWORD / $EMAIL ]<br />";
    
        $user["ID"] = "$ID";
        $user["NAME"] = "$NAME";
        $user["PASSWORD"] = "$PASSWORD";
        $user["EMAIL"] = "$EMAIL";
    
        array_push($users, $user);
      }
    }
    else
    { echo"[ no data ]"; }
    
    print_r($users);
    echo json_encode($users);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭