【问题标题】:How to make two table inner join with json如何使用json进行两个表内连接
【发布时间】:2020-01-13 07:57:05
【问题描述】:

我正在使用 vuejs 库,想通过组合两个表从 mysql 获取数据。使用 INNER JOIN 时无法从 json 获取数据。你能检查一下代码吗?

谢谢

$list = $db->query("SELECT*FROM table1 as u INNER JOIN table2 as p ON u.id = p.userID WHERE p.contentID = '$cID');
$item = $list->first();

$res = array();
$res[] = $item;

$row['respons'] = $res;

header("Content-type: application/json");
echo json_encode($row);
die();

表 1 名称(示例):用户

id:1 - 用户名:Serkan

id:2 - 用户名:Jack

id:3 - 用户名:Rose

表 2 名称(示例):内容

id:x - userID: 1 - content: 这是mysql内容

id:x - 用户 ID: 3 - 内容:新的 mysql 内容

【问题讨论】:

标签: php mysql json inner-join


【解决方案1】:

你有很多语法和拼写错误,你也应该阅读更多关于 sql 查询如何与 php 一起工作的内容。如果您的“cID”变量是用户输入,您还应该在将此输入发送到数据库服务器之前对其进行清理,以避免 sql 注入攻击。

$list = $db->query("SELECT*FROM table1 as u INNER JOIN table2 as p ON u.id = p.userID WHERE p.contentID = '$cID';");

$res = [];

if ($list ->num_rows > 0) {
 while ($row = $result->fetch_assoc()) {
  array.push($res, $row['response'])
 }
{

header("Content-type: application/json");
echo json_encode($res);
die();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多