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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?