doumindang2416 2012-12-17 19:55
浏览 28
已采纳

mysql_fetch问题......我疯了

I'm sorry, probably somewhere there will be the answer to my question, but it's hours I'm looking for trying to resolve this problem: Here is the code:

    <?php
    $con = mysql_connect("****","****","***");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

        mysql_select_db("******", $con);

    $query = "SELECT id FROM fq_questions";
    $rowcnt = mysql_num_rows(mysql_query($query));      
    echo "Tot scenes: ".$rowcnt;
    $id = rand (1,$rowcnt);
    echo "<br>Id rand: ".$id."<br>";    
flush();
    $newquery = "SELECT question FROM fq_questions WHERE id=".$id;
    $result = mysql_query($newquery);
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "
";
        $message .= 'Whole query: ' . $newquery;
        die($message);
    }

    $row = mysql_fetch_assoc($result);
    echo $row['question'];

    mysql_close($con);
    ?>

The problem is that there is no output. I've tried everything, seems to be a problem into the query, but there is a result, it's not false, but even if it exists, nothing is outputted. The code works till

    echo "<br>Id rand: ".$id."<br>";    

then it shows nothing. It's a dummy problem, i'm getting crazy just because of it.

Uh, was forgetting... The website where I've got the problem: http://www.freelabs.it/filmquiz/game.php

  • 写回答

3条回答 默认 最新

  • doxd96148 2012-12-17 20:01
    关注

    "desc" is a MySQL reserved word, you should just change that column name in your DB. Anyway, your method is not random at all, and it will fail as soon as you have a "hole" in your ids (when you delete one member).

    Take a look at MySQL "ORDER BY RAND()"

    $data = mysql_query("SELECT description FROM fq_questions ORDER BY RAND() LIMIT 1");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并