2012-04-28 90 views
1

我有一個問題我的腳本有三個mysql_query應後相互使用,我想創建一個腳本,預訂門票通過改變他們的身份售出=「無」到「有」 ,該腳本計算用戶在html表單上輸入的票數,該表單向服務器端提供了一個變量,其中包含票數= $ticketsPHP的MySQL fetch語句獲取問題

提示:這是這樣一個模型,以便無需MySQL的注射安全

這裏是我的代碼:

//get ticket status 
    $eventTicket = mysql_query("SELECT eventTickets FROM beventreservation WHERE eventId = '$eventId'") or die(mysql_error()); 
    $ticketrow = mysql_fetch_array($eventTicket) or die(mysql_error()); 


    //test... which is working ! 
    echo $ticketrow['eventTickets']; 


    //get classId from classes 
    $selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());  

    $classrow = mysql_fetch_array($selectClass) or die(mysql_error()); 

    //this var is to define which class the user used 
    $choosedClass = $classrow['classId']; 

//test ... which did not work !!! 
echo $classrow['classId']; 

     if ($ticketrow['eventTickets'] == "Yes") 

    { 
     for($counter=1;$counter<$numberOfTickets;$counter++) 

     { 
      $bookTicket = mysql_query("UPDATE unites SET ticketSold = 'Yes' WHERE businessreservationIdFk = '$eventId' AND classIDfk ='$choosedClass'") or die(mysql_error()); 
      echo "ticket ". $counter . " done !"; 


     } 

    } 

腳本不取此語法,並沒有錯誤顯示在我的頁面上!

$classrow = mysql_fetch_array($selectClass) or die(mysql_error()); 

還,我想這句法後呼應變量$tickets,它沒有露面,有沒有要讀取的mysql_query多個相同的腳本頁面上的問題嗎?請告訴我,我在哪裏錯了。

回答

4

不要用mysql_fetch_*()電話呼叫die()結合。如果沒有返回行,mysql_fetch_array()返回FALSE,這將觸發你的die()並殺死即使沒有錯誤的腳本。既然你已經穿上錯誤上$selectClassmysql_query()電話檢查,你知道它已經成功了。

// This query returned no rows, but was successful syntactically and functionally. 
$selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());  

相反,測試,如果被返回的行:

if (mysql_num_rows($selectClass) > 0) { 
    // Fetch and do other stuff 
    $classrow = mysql_fetch_array($selectClass); 
    $choosedClass = $classrow['classId']; 
    // etc... 
    // etc... 
} 
else { 
    // Do whatever you need to do if no rows return 
}