我创建了一个数组并将其存储在 $_SESSION 变量中,现在我想将该数组包含在 MySQL select 语句中,以根据 p 获取更多信息,p 包含在我的 URL 中。我的代码包含在 HTML 页面中,除了必要的 HTML 和 session_start 之外,该页面完全是空的,所以这不是问题。代码如下所示:
<?php
$p = $_GET["p"];
var_dump($_SESSION['questions'][$p]);
include("../script/db_connect.php");
$p = $_GET["p"];
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]}'";
$question_infos = mysqli_query($con, $select_right_question);
while ($row = mysqli_fetch_assoc($question_infos)) {
echo $row["question"] . '</br>'
. $row["right_answer"] . '</br>'
. $row["answer2"] . '</br>'
. $row["answer3"] . '</br>'
. $row["answer4"];
}
mysqli_close($con);
?>
我已经测试过它,主要问题是以下行:
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]}'";
在这里包含 SESSION 变量的正确方法是什么?
最佳答案
忽略代码的安全问题,并假设 $_SESSION['questions'][$p]
返回一个数组,您可以将 SQL 语句修改为:
$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]['id']}'";
关于php - 脚本不断显示数组到字符串转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726557/