获取Inner Join的所有列

时间:2017-11-27 17:13:13

标签: php inner-join

我需要获取两个表中的所有列。但我似乎无法获取数据是我的PHP有什么问题或者是我的JavaScript。

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
header("Content-Type: application/json; charset=UTF-8");

include("global.php");

$conn = new mysqli(server, dbuser, dbpw, db);
$userid = $_GET['userid'];

$result = $conn->query("SELECT * FROM profiles INNER JOIN StudentProfile ON profiles.userid = StudentProfile.userid");

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"userid":"'  . $rs["userid"] . '",';
    $outp .= '"parentname":"' . $rs["parentname"] . '",';
    $outp .= '"profilepic":"' . $rs["profilepic"] . '",';
    $outp .= '"contact":"' . $rs["contact"] . '",';
    $outp .= '"address":"' . $rs["address"] . '",';
    $outp .= '"studentid":"' . $rs["studentid"] . '",';
    $outp .= '"studentname":"' . $rs["studentname"] . '",';
    $outp .= '"sclass":"'   . $rs["sclass"]        . '"}';
    $outp .= '"sprofilepic":"' . $rs["sprofilepic"] . '",';
}
$outp .="]";



$conn->close();

echo($outp);
?>

studentprofile profiles

2 个答案:

答案 0 :(得分:0)

1。你可以用fetch_all来做。为此你必须有mysqlnd驱动程序

$result = $conn->query("SELECT * FROM profiles INNER JOIN StudentProfile ON profiles.userid = StudentProfile.userid");

$output = $result->fetch_all(MYSQLI_ASSOC);
$jsonOutput = json_encode($output);

2。带循环

$result = $conn->query("SELECT * FROM profiles INNER JOIN StudentProfile ON profiles.userid = StudentProfile.userid");

$output = [];
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    $output[] = $rs;
}
$jsonOutput = json_encode($output);

答案 1 :(得分:0)

问题在于您的代码中的JavaScript ......

在循环中查看最后2行代码

只需替换

  

$ outp。='“sclass”:“'。$ rs [”sclass“]。'”}';

     

$ outp。='“sprofilepic”:“'。$ rs [”sprofilepic“]。'”,';

使用

  

$ outp。='“sclass”:“'。$ rs [”sclass“]。'”,';

     

$ outp。='“sprofilepic”:“'。$ rs [”sprofilepic“]。'”},';