通过使用angularjs和sql

时间:2016-02-26 09:35:03

标签: php sql angularjs json

我通过比较电子邮件从数据库中获取数据,但是如果该电子邮件有多个记录,我编写的代码只会获得一条记录。

$res="SELECT *  FROM user_salary_details  WHERE email ='$userEmailid '";
$result=mysql_query($res);  

if($row = mysql_fetch_array($result)) {
     $city=$row["city"];
     $state=$row["state"];
     $pin=$row["pincode"];

     $data = array(
                    "success"   =>  "success",
                 "city"         => $city,
                 "state"        => $state,
                 "pin"          =>$pin,
              );

    echo json_encode($data);         
}

1 个答案:

答案 0 :(得分:0)

请改用此代码:

// YOU FORGOT TO CONNECT
$link = mysqli_connect("localhost", "my_user", "my_password", "world");


if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}


$query = "SELECT * FROM user_salary_details WHERE email='$userEmailid '";

// YOU NEED TO PASS CONNECTION AS FIRST ARGUMENT    
if(!$result = mysqli_query($link, $query)){ // Use mysqli
    die 'Query Error'; // Exit script on error
}

$data = [];
while($row = mysqli_fetch_assoc($result)){ // Loop trough all rows

     $rowData = $row;
     $rowData['success'] = 'success';
     $rowData['pin'] = $row['pincode'];
     unset $rowData['pincode'];

     $data[] = $rowData; // Add row to total data       
}
echo json_encode($data); // echo all data