基本上我想要做的是將變量分配給SQL語句的結果。爲SQL查詢的結果分配一個PHP變量
在這個例子中,我想計算當前登錄的客戶ID出現在稱爲預訂的表中的次數。如果此計數爲5或以上,則$ apply_discount = 50。這可以稍後在頁面中使用以折扣百分比數量。
我已經嘗試了幾天,但我很不熟悉SQL使用PHP。
這是我到目前爲止有:
<?php
$numofbookings = " number of times {$_SESSION['login']} appears in the booking table " //this is where im unfamiliar with the syntax
// sql statement that counts how many times the currently logged in customers id {$_SESSION['login']} appears in the booking table
// database connection is done earlier on in the page with a db_connection.php include
// table is called "bookings"
// col that contains customer ids (which comes from {$_SESSION['login']}) is called customer_id
?>
<?php
if $numofbookings =("5>")
{
$apply_discount=("50");
}
else
{
$apply_discount=("0");
}
// if the customer has 5 of more bookings then $apply_discount is 50,
// this can be used later on to discount the % amount from the base price
?>
<?php
$newprice = $base_price * ((100-$apply_discount)/100); // calculation at bottom of page
?>
您的查詢是哪裏? – MichaelRushton
如果你對字符串進行計算並且涉及金錢,那麼最好使用二進制計算器:-S – DanFromGermany
注意:如果$ numofbookings =(「5>」),你應該做'if($ numofbookings> 5)'而不是'' '你所引用的數字不會被視爲整數。 –