【问题标题】:Fetching all columns of Inner Join获取内连接的所有列
【发布时间】:2017-11-27 17:13:12
【问题描述】:

我需要获取两个表中的所有列。但我似乎无法获取数据是我的 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

【问题讨论】:

标签: php inner-join


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    查看while循环中的最后两行代码

    只是替换

    $outp .= '"sclass":"' . $rs["sclass"] . '"}';

    $outp .= '"sprofilepic":"' . $rs["sprofilepic"] . '",';

    $outp .= '"sclass":"' . $rs["sclass"] . '",';

    $outp .= '"sprofilepic":"' . $rs["sprofilepic"] . '"},';

    【讨论】:

    • 如果我使用带有内部连接的数组@kunal 没问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多