[英]How to use PHP variables in MySQL queries with WHILE fetched rows?
I have successfully done the php script that connect to my database and return desired data. 我已成功完成连接到我的数据库并返回所需数据的php脚本。 But i need to extend this script to insert a variable that will show the price with some calculations (tax,no-tax,margin etc.).
但我需要扩展此脚本以插入一个变量,该变量将通过一些计算(税,免税,保证金等)显示价格。
This script show me only price value of the first database row 0.00 in all fetched rows and its not correct - for the first database row is ok because product have 0.0000 price, but the other rows are filled with correct values. 此脚本仅向我显示所有已获取行中第一个数据库行0.00的价格值且不正确 - 因为第一个数据库行正常,因为产品的价格为0.0000,但其他行填充了正确的值。 It seems like the while loop dont like my $styledprice variable .
似乎while循环不喜欢我的$ styledprice变量。 I can't figure how to show correct field values in all lines.
我无法想象如何在所有行中显示正确的字段值。 Any ideas much apppreciated?
任何想法都很适用? I'm a PHP beginner!
我是PHP初学者!
$pricequery = "SELECT rrp FROM my_products";
$b2bprice = mysql_query($pricequery);
$rrps = mysql_fetch_array($b2bprice);
$price = $rrps['rrp'];
$styledprice = number_format($price, 2, '.', '');
$query = mysql_query("
SELECT
CONCAT(' <td> ',p.id,' </td><td> ',p.manufacturer,' </td><td> ',p.reference,' </td><td> ',p.name,' </td><td> ',p.quantity,' <td> ','".$styledprice."',' </td> ') AS row
FROM my_products p
");
echo "
<table>
<tr>
<td><h5>ID</h5></td>
<td><h5>Manufacturer</h5></td>
<td><h5>PN</h5></td>
<td><h5>Name</h5></td>
<td><h5>Quantity</h5></td>
<td><h5>Price</h5></td>
</tr>";
while($row=mysql_fetch_array($query))
{
echo "<tr>".$row['row']."</tr>";
}
echo "
</table>";
Yes i know about mysql_ functions that are deprecated. 是的我知道不推荐使用的mysql_函数。
Something like this should work better. 这样的事情应该会更好。 It seperates the resultset from the database and the output via HTML.
它通过HTML从数据库和输出中分离结果集。
// Get the Result Set
$result = mysql_query("SELECT p.id, p.manufacturer, p.reference, p.name, p.quantity FROM my_products p");
// Convert the rows and columns from the Result Set to a PHP Array
$data = array(); // empty array
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
// Now you have access any row or column
echo "<table>";
foreach($data as $row){
// prepare the data
$formatttedQuantity = number_format($row['quantity'], 2, '.', '');
// show each Table Row
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "<td>" . $row['reference'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $formatttedQuantity . "</td>";
echo "</tr>";
}
echo "</table>";
It looks like you can do this all in one sql command like: 看起来你可以在一个sql命令中完成所有这些,如:
<?php
$sql = "SELECT * FROM my_products";
$result = mysql_query( $sql );
echo "
<table>
<tr>
<td><h5>ID</h5></td>
<td><h5>Manufacturer</h5></td>
<td><h5>PN</h5></td>
<td><h5>Name</h5></td>
<td><h5>Quantity</h5></td>
<td><h5>Price</h5></td>
</tr>";
while( $row=mysql_fetch_array( $result ) ){
$price=$row['rrp'];
$styledprice = number_format( $price, 2, '.', '' );
echo "
<tr>
<td>{$row['id']}</td>
<td>{$row['manufacturer']}</td>
<td>{$row['reference']}</td>
<td>{$row['name']}</td>
<td>{$row['quantity']}</td>
<td>{$styledprice}</td>
</tr>";
}
echo "
</table>";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.