douci1918 2013-06-30 19:08
浏览 49
已采纳

我在数据库中的while循环是重复结果。 我知道这是我格式不好的查询。

I have a query to bring results from my database. It works... until there are more than 2 results that it, then it just repeats some results before adding in new ones.

I know it will be because my query is fairly poor, can anyone advise me?

The idea is

  1. connect to database with photo links
  2. get the default user picture as $profile_main
  3. join the words "photo_" with the default picture number and call it $answer (ex: column 'photo_1' in database)
  4. now check the database again and get the results for $answer and output all information from that database column.
$result = mysqli_query($con,"SELECT * FROM tbl_photos");
while($row = mysqli_fetch_array($result))
    {
    $profile_main = $row['default'];
    $answer = "photo_" . $profile_main;

        $result2 = mysqli_query($con,"SELECT $answer FROM tbl_photos");
        while($row = mysqli_fetch_array($result2))

        {
            echo "<img src=\"" . $row[0] . "\">";
        }
    }
  • 写回答

4条回答 默认 最新

  • douyi8760 2013-06-30 20:27
    关注

    mysql_fetch_row returns numerical indexes instead of column names (so ['default'] just won't work)...

    This is how I would do it if I'm understanding you correctly:

    $result = mysqli_query($con,"SELECT * FROM tbl_photos");
    while($row = mysqli_fetch_assoc($result))
        {
        $answer = $row['photo_'.$row['default']];
        echo "<img src=\"" . $answer . "\">";
        }
    

    Anyway, this is assuming default and photo_x are in the same row.

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证