2014-01-27 2 views
-3

기본적으로 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 
?> 
+0

어디 계시나요? – MichaelRushton

+0

만약 문자열에 대한 계산을하고 관련 돈이 있다면 이진 계산기를 더 잘 사용하십시오 : -S – DanFromGermany

+0

Sidenote : $ numofbookings = ("5>") 대신'if ($ numofbookings> 5) '인용 된 숫자는 정수로 취급되지 않습니다. –

답변

0

먼저 당신이 당신의 요구 사항을 충족하는 행의 수를 계산하기 위해 데이터베이스를 조회해야합니다

내가 지금까지 무엇을 가지고 할인 로직 :

if ($numofbookings > 5) 
{ 
    $apply_discount = 50; 
} 

else 
{ 
    $apply_discount = 0; 
} 

또한 세션을 사용할 때 session_start();이 파일의 맨 위에 포함 된 경우 $_SESSION은 정의되지 않습니다.

당신은 mysqli 또는 PDO 확장으로 보여야의 MySQL 문서뿐만 아니라, PHPcontrol structuressessions.

+0

사용 된 모든 파일에서 session_start();가 필요하다는 것을 보여주는 추가 된 참고일지도 모릅니다. ;-) 나는 PHP/SQL에서 OP의 경험을 의심 스럽다. –

+0

좋은 지적, 고마워. – MichaelRushton

+0

반갑습니다. –