提问人:Maina David 提问时间:2/20/2021 最后编辑:StrawberryMaina David 更新时间:2/20/2021 访问量:478
如何在php中将多行作为json传递
How to pass multiple rows as json in php
问:
我正在尝试将行详细信息作为 JSON 传递。如何将所选列与查询分开
$SQLTask = "
SELECT priority
, task_name AS title
, task_description AS description
, start_date AS start
, end_date AS end
FROM t_tasks
"; -- removed extraneous semi-colon
$RESULT_Task = mysqli_query( $conn, $SQLTask );
while($ROW_TASK = mysqli_fetch_assoc($RESULT_Task))
{
$TASK_NAME = $ROW_TASK['title'];
$TASK_DESC = $ROW_TASK['description'];
$TASK_START = $ROW_TASK['start'];
$TASK_END = $ROW_TASK['end'];
}
$TASK_DETAILS[] = array('title' => $TASK_NAME, 'description' => $TASK_DESC, 'start' => $TASK_START, 'end' => $TASK_END);
mysqli_close($conn);
echo json_encode($TASK_DETAILS);
以下面的格式尝试了它,它有效,但我需要将优先级列用于其他查询。 JSON 用于馈送日历并显示事件。
<?php
require('../classes/class_connect.php');
$Project_ID = $_GET['id'];
$SQLTask = "SELECT task_name AS title, task_description AS description, start_date AS start, end_date AS end FROM t_tasks WHERE project_id = " . $Project_ID . "";
$RESULT_Task = mysqli_query( $conn, $SQLTask );
while($ROW_TASK = mysqli_fetch_assoc($RESULT_Task))
{
$TASK_ARRAY[] = $ROW_TASK;
// if($ROW_TASK['priority'] == 'Low')
// {
// $eventClassNames = "bg-light-secondary";
// }
// else if($ROW_TASK['priority'] == 'Normal')
// {
// $eventClassNames = "bg-light-primary";
// }
// else if($ROW_TASK['priority'] == 'High')
// {
// $eventClassNames = "bg-light-warning";
// }
// else if($ROW_TASK['priority'] == 'Urgent')
// {
// $eventClassNames = "bg-light-danger";
// }
// array_push($TASK_ARRAY,$eventClassNames);
}
mysqli_close($conn);
echo json_encode($TASK_ARRAY);```
答:
-1赞
ANOL GHOSH
2/20/2021
#1
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);
/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
/* associative and numeric array */
$row = $result->fetch_array(MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
echo json_encode($row)
编号:https://www.php.net/manual/en/mysqli-result.fetch-array.php
1赞
Clarence Bitegeko
2/20/2021
#2
我不确定我是否清楚地理解了您的问题以及您想要实现的“如何将所选列与查询分开”的目标。
但是,对于您说明的第二部分,由于您想要的只是优先级字段,因此您可以从查询中删除所有其他字段。此外,还可以在查询中添加 eventClassNames。
尝试使用 mysqli 的fetch_all,看看它是如何工作的。对于第一部分,除非对数组进行进一步处理,否则您不必遍历结果集来创建数组 https://www.php.net/manual/en/mysqli-result.fetch-all.php
$SQLTask = "SELECT priority,
case priority
when 'Low' then 'bg-light-secondary'
when 'Normal' then 'bg-light-primary'
when 'High' then 'bg-light-warning'
when 'Urgent' then 'bg-light-danger'
end AS eventClassName
FROM t_tasks WHERE project_id = " . $Project_ID . "";
$RESULT_Task = mysqli_query( $conn, $SQLTask );
$TASK_ROWS=$RESULT_TASK->fetch_all(MYSQLI_ASSOC);
$TASK_ARRAY=array_column($TASK_ROWS,'eventClassName');
echo json_encode($TASK_ARRAY);
评论
1赞
Clarence Bitegeko
2/21/2021
@WaihuraMaina 在你对你提到的JSON输出只有一行的问题的评论中,这是因为你在while循环中声明了$TASK_ARRAY,所以它每次都会被初始化,在lop $TASK_ARRAY=Array()之前声明它,然后用array_push($TASK_ARRAY,$eventClassNames)填充它;在循环中,就像你所做的那样。
评论
[{"title":"Test Task","description":"This is a test task","start":"2021-02-19","end":"2021-02-20"}]
并bind_param
将任何数据添加到查询中。不要使用字符串插值或串联来实现此目的,因为您已经创建了严重的 SQL 注入错误。切勿将 或任何类型的数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。mysqli
$_POST
$_GET
$TASK_ARRAY