douyan1321 2014-04-16 14:35
浏览 15
已采纳

如何创建一个mysql结果数组,其行中具有一定的值

I have following code:

<?php
include_once 'init/init.funcs.php';
$pollid=$_GET['pollid'];
$result = mysql_query('SELECT question FROM questions where survey_id="' . $pollid . '"');
$question = mysql_result($result, 0);
echo $pollid;
echo $question;

?>

And it will echo first question which has survey_id=$pollid. But I want to make an array of all the questions, which have survey_id=$pollid.How I can do that?

  • 写回答

2条回答 默认 最新

  • dqch34769 2014-04-16 14:37
    关注

    Just loop through the results and add them to an array:

    $pollid = (int) $_GET['pollid'];
    $questions = array();
    $result = mysql_query('SELECT question FROM questions where survey_id="' . $pollid . '"');
    while($row = mysql_fetch_assoc($result)) {
        $questions[$pollid] = $row['question '];
    }
    

    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

    You are also wide open to SQL injections. In my example I casted $_GET['pollid'] to an integer to help protect against them. A better approach would be to use prepared statements as mentioned above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错