douyousu9691 2012-11-26 06:14
浏览 38
已采纳

如何从mysql输出两列值并循环其余部分

In php working with mysql, how can I output two columns values only once, and then loop thru the others.

Here:

$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
echo $row['category'];    //Only once
echo $row['subcategory']; //Only once

{
echo $row['title'];
echo $row['body'];
echo $row['views'];
}

Desired output:

Category->SubCategory //Outputted only once

Title,  //Title Body and Views depend on whatever's there in the db. Could be 10 or more
Body,
Views

Title,
Body,
Views

Title,
Body,
Views
  • 写回答

4条回答 默认 最新

  • dryl34156 2012-11-26 06:19
    关注

    This code shoud do the trick

        $exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
        $row_once = mysql_fetch_array($exe_fetch_data)
        echo $row_once['category'];    //Only once
        echo $row_once['subcategory']; //Only once
    
       while($row = mysql_fetch_array($exe_fetch_data)) {
        echo $row['title'];
        echo $row['body'];
        echo $row['views'];
        }
    

    You could also try using a flag whose value is initially true and will turn false after first iteration. But I think above code will be faster than using a flag as it wont compare/check flag value after each iteration and with a huge database it will save execution time.

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

报告相同问题?

悬赏问题

  • ¥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关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA