douhu2131 2017-03-10 19:45
浏览 60
已采纳

脚本一直显示数组到字符串转换错误

I created an array and stored it inside a $_SESSION variable, now I want to include that array inside a MySQL select statement to get more infos depending on p, which is included in my URL. My code is included inside a HTML page, which is completely empty except for necessary HTML and of course session_start, so this is not the problem. The code looks like that:

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

I already tested it and the main issue is the following line:

$select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]}'";

What is the correct way to include the SESSION variable here?

  • 写回答

1条回答 默认 最新

  • donglin9068 2017-03-10 20:01
    关注

    Disregarding the security issues with your code, and assuming $_SESSION['questions'][$p] is returning an array, you can modify your SQL statement to:

    $select_right_question = "select * from questions where id = '{$_SESSION['questions'][$p]['id']}'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容