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']}'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?