php脚本只在android studio中显示一行数据而不是很多

时间:2016-04-25 07:57:13

标签: php mysql

我想问一下如何显示多个row个数据而不是一个row。以下代码仅显示一行记录而不是多条记录。我在这里使用mysqli_prepare语句。或问题是我的android studio编码?我的应用程序使用登录功能和编码实现,如下所示。

<?php
$host="DB_HOST";
$user="DB_USER";
$password="DB_PASSWORD";
$db="DB_NAME";

$con = mysqli_connect($host,$user,$password,$db);

$parentic=$_POST["ParentIC"];
$password=$_POST["Password"];

$selectquery = mysqli_prepare($con, "SELECT Parents_Data.Name, Student_List.StudName, Student_List.StudIC, Student_List.Form,Student_List.Class,discipline_record.Date,discipline_record.RulesCode, discipline_record.TypesofMistakes,discipline_record.Punishment FROM discipline_record LEFT JOIN Student_List ON discipline_record.StudIC = Student_List.StudIC LEFT JOIN Parents_Data ON Student_List.ParentIC = Parents_Data.ParentIC WHERE Parents_Data.ParentIC = ? AND Parents_Data.Password = ? ");
mysqli_stmt_bind_param ($selectquery, "ss", $parentic, $password);
mysqli_stmt_execute($selectquery);

mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery,$name,$studname,$studic,$form,$classs,$ddate,$code,$mistakes,$punishment);

$user = array();



while(mysqli_stmt_fetch($selectquery))
{
    $user[name]=$name;
    $user[studname]=$studname;
    $user[studic] = $studic;
    $user[form]=$form;
    $user[classs]=$classs;
    $user[ddate]=$ddate;
    $user[code]=$code;
    $user[mistakes]=$mistakes;
    $user[punishment]=$punishment;            


}

    echo json_encode($user);

    mysqli_stmt_close($selectquery);


mysqli_close($con);

?>                          

3 个答案:

答案 0 :(得分:1)

我会选择类似的东西:

$userGroup = array();
$user = array();

while(mysqli_stmt_fetch($selectquery))
{
    $user[name]=$name;
    $user[studname]=$studname;
    $user[studic] = $studic;
    $user[form]=$form;
    $user[classs]=$classs;
    $user[ddate]=$ddate;
    $user[code]=$code;
    $user[mistakes]=$mistakes;
    $user[punishment]=$punishment;

array_push($userGroup,$user);            


}

echo json_encode($userGroup);

答案 1 :(得分:1)

使用二维数组可能更容易

$users = array();

while(mysqli_stmt_fetch($selectquery))
{
    $users[] = array();
    $users[][name]=$name;
    $users[][studname]=$studname;
    $users[][studic] = $studic;
    $users[][form]=$form;
    $users[][classs]=$classs;
    $users[][ddate]=$ddate;
    $users[][code]=$code;
    $users[][mistakes]=$mistakes;
    $users[][punishment]=$punishment;         

}

foreach ( $users as $user )
   echo json_encode($user);

答案 2 :(得分:0)

看起来你正在进行正确的查询,但是在使用一个平面数组而不是二维获取结果时。

另外,尽量避免公开发布您的数据库凭据:)这非常危险。

所以你应该做那样的事情:

if (==null)