+----+------------+---------+-----------+
| id | type_id | grammage| price |
+----+------------+---------+-----------+
| 1 | 1 | 20 | 44 |
| 2 | 1 | 50 | 80 |
| 3 | 1 | 100 | 110 |
| 4 | 1 | 250 | 180 |
| 5 | 1 | 500 | 250 |
+----+------------+---------+-----------+
我想选择每克的价格例子:选择150克
使用laravel数据库构建器:
$weight = DB::table('weight')->where('grammage', '150')->first();
echo $weight->price;
试试这个:
$con = mysqli_connect(...);
$arrayValues = array();
$result = mysqli_query($con, "SELECT `price`, `grammage` FROM `exampleTable`");
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo 'Grams: '.$row[1].'g Price: '.$row[0];
echo ' Price per gram: '.round($row[0]/$row[1],2).'<br/>';
}
}