기본적으로 SQL 문 결과에 변수를 지정하려고합니다.SQL 쿼리의 결과에 PHP 변수 할당
이 예제에서는 현재 로그인 한 고객 id가 예약이라는 테이블에 몇 번이나 나타나는지 계산하려고합니다. 이 수가 5 이상이면 $ apply_discount = 50입니다. 나중에 페이지에서 사용하여 백분율 금액을 할인 할 수 있습니다.
나는 며칠 동안 노력했지만 지금은 SQL withing PHP를 사용하는 것에 익숙하지 않다. 당신이 당신의 문제를 해결해야하는 그런
$mysqli = new mysqli('hostname', 'username', 'password', 'database');
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM bookings WHERE customer_id = ?")
$stmt->bind_param('i', $_SESSION['login']);
$stmt->execute();
$stmt->bind_result($numofbookings);
$stmt->fetch();
$stmt->close();
:
<?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
Sidenote : $ numofbookings = ("5>") 대신'if ($ numofbookings> 5) '인용 된 숫자는 정수로 취급되지 않습니다. –