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>";
}
}
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>";
}
}
Your $resultset
is coming only from the first query ( $query = "SELECT id,name,price FROM $table_id";
) and not the others?
This section:
$id = "SELECT id FROM $table_id";
$name = "SELECT name FROM $table_id";
$price = "SELECT price FROM $table_id";
is actually useless, as it is just assigning strings to $id
, $name
and $price
.
Did you mean?:
$result_set= mysql_query($query);
while ($row = mysql_fetch_assoc($result_set))
{
echo "<td><tr> ".$row['id']." ".$row['name']." ".$row['price']." </tr></td>";
}
$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_*
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
You do not need to have three separate selects for id, name and price. Just use:
if ($result_set) {
$row = $result_set -> fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$price = $row['price'];
}
Then you can input it into your table or do whatever you would like with the info. The fetch_assoc()
will only work if you are using MySQLi . mysql_fetch_assoc
is deprecated and you should switch to either PDO or MySQLi
<?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( );
}
That seems more like it should work :)
EDIT : by the way, use of mysql_* is highly discouraged, as they will disappear in upcoming versions of PHP. Start using PDO or MySQLi instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.