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

时间:2017-04-06 19:55:21

标签: php android mysql arrays

我有一个' Android'我需要从数据库中获取一些信息的项目。数据库是“MySQL'”。我必须通过web服务,因为我之前从未做过任何PHP,所以我几乎看不到问题,因为逻辑对我来说似乎没问题。

这是我的代码:

<?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();
?>

我目前在我的数据库中有超过6个用户,但我似乎无法全部获得这些用户。我留下了一些我的代码注释,所以你可以看到我尝试过的,唉没有任何成功。我想要回复一个JSON&#39;我的所有用户都在其中的数组。

你们有没有想过如何继续?

2 个答案:

答案 0 :(得分:1)

这适用于恕我直言

<?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 :(得分:0)

我想知道你是否有一个范围错误,tbh我不是完全最新的PHP范围规则所以试试看。

 <?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();

             $users = array();// maybe you have scoping error, put this here
            if (!empty($result))
            {
                if($result->num_rows > 0) 
                {
                    $response['success'] = 1;
                    #$response['user'] = 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
                        #$response['users'][] = $user;

                        #echo json_encode($user);
                       // array_push($users, $user);
                        $users[] = $user;
                    }
                }
                else
                {
                    $response['success'] = 0;
                    $response['message'] = 'No user found';
                }
            }
            else
            {
                $response['success'] = 0;
                $response['message'] = 'No user found';
            }

            $response['users'] = $users;
            $conn->close();

            return json_encode($response);
    ?>