dragon8002 2012-08-02 05: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部