dragon8002 2012-08-02 13:33
浏览 28
已采纳

PHP Select语句需要快速更正

My echo statement at the end is certainly wrong, I've put it in just to show what I am trying to do, I'd simply like to to call these three variables as set previously. What would be the right way to do that?

$table_id = 'mynewtable';
$query = "SELECT id,name,price FROM $table_id";
$result_set= mysql_query($query);

$id = "SELECT id FROM $table_id";
$name = "SELECT name FROM $table_id";
$price = "SELECT price FROM $table_id";

if ($result_set){

$record= mysql_fetch_row($result_set);
$idrecord= mysql_fetch_row($id);
$namerecord= mysql_fetch_row($name);
$pricerecord= mysql_fetch_row($price);

        foreach ( $record as $value){
        echo "<td><tr> $idrecord $namerecord $pricerecord </tr></td>";
    }
}
  • 写回答

6条回答 默认 最新

  • dscrn1974 2012-08-02 13:38
    关注

    There's no point to fetch each of the elements separately. Use mysql_fetch_assoc to get an array with the columns specified in SELECT statement, as keys. this way you can easily get the echo you're after, without querying the database unnecessarily

    $table_id   = 'mynewtable';
    $query      = "SELECT id,name,price FROM $table_id";
    $result_set = mysql_query($query);
    
    if($result_set)
    {
        while ($row = mysql_fetch_assoc($result_set)) {
            echo "<td><tr>" .$row["id"]
            . " ". $row["name"]
            . " " .$row["price"] . "</tr></td>";
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大