0

我最后的回声语句肯定是错误的,我把它放进去只是为了展示我想要做什么,我只是想把这三个变量称为之前设置的。这样做的正确方法是什么?

$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>";
    }
}
4

6 回答 6

3

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>";
    }

}
于 2012-08-02T13:38:13.150 回答
1
$table_id = 'mynewtable';
$query = "SELECT id,name,price FROM $table_id";
$result_set= mysql_query($query);

if (mysql_num_rows() > 0){

    while($row = mysql_fetch_array($result_set)) {
        echo $row['id'] .", ". $row['name'] .", ". $row['price'];
    }

}

Please start using mysqli_* instead of mysql_*

于 2012-08-02T13:38:06.107 回答
1

$resultset仅来自第一个查询 ( $query = "SELECT id,name,price FROM $table_id";) 而不是其他查询?

本节:

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

实际上没用,因为它只是将字符串分配给$id,$name$price.

你的意思?:

$result_set= mysql_query($query);
while ($row = mysql_fetch_assoc($result_set)) 
{
    echo "<td><tr> ".$row['id']." ".$row['name']." ".$row['price']." </tr></td>";
}
于 2012-08-02T13:35:04.847 回答
0

why not

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

if ($result_set){
        while( $row = mysql_fetch_array(result_set)){ ?>
        <td>
          <tr> <?php echo $row['id'] . ' ' $row['name'] . ' ' . $row['price']; ?> </tr>
        </td>
<?php
    } // end of while
} // end of if
于 2012-08-02T13:38:36.497 回答
0

您不需要对 id、name 和 price 进行三个单独的选择。只需使用:

if ($result_set) {
    $row = $result_set -> fetch_assoc();
    $id = $row['id'];
    $name = $row['name'];
    $price = $row['price'];
}

然后你可以将它输入到你的表格中,或者对信息做任何你想做的事情。fetch_assoc()仅当您使用MySQLi时才有效。mysql_fetch_assoc弃用,您应该切换到PDOMySQLi

于 2012-08-02T13:40:33.767 回答
0
<?php
$query = "SELECT id,name,price FROM mynewtable";
$result_set = mysql_query($query);

if ($result_set!==false){
    while( $row = mysql_fetch_assoc( $result_set ) ) {
     echo '<td>' $row['id'] . ' ' . $row['name'] . ' ' . $row['price'] . '</td>';
    }
}
else {
    echo "Something went wrong with the query:";
    echo mysql_error( );
}

这似乎更像它应该工作:)

编辑:顺便说一句,强烈建议不要使用 mysql_*,因为它们将在即将发布的 PHP 版本中消失。开始使用 PDO 或 MySQLi。

于 2012-08-02T13:40:38.590 回答