2012-11-20 47 views
0

我有一個文本框,它從數據庫中獲取最大值。但它給了錯誤的價值。MYSQL/PHP查詢沒有給出正確的值

你能幫忙弄清楚這裏出了什麼問題嗎?

basicInformation功能:

private function basicInformation() { 
      // initialize variables 
    $host = "localhost"; 
    $db_name = "test"; 
    $tbl_name = "ballpark_details"; 
    $username = "root"; 
    $password = ""; 

    // connect to database 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); # ORDER BY ballpark_details_id DESC 
    $query = mysql_query("SELECT MAX(ballpark_details_booking_ref) as max_booking_ref FROM `ballpark_details`"); 
    //Getting the max ref_id 
    $values = mysql_fetch_assoc($query);  
    while ($query !=-1 && $row = mysql_fetch_assoc($query)) { 
    $max = $row['max_booking_ref']; 
    } 
    echo print_r($values, true); 
    $html = ""; 
    $html .= '<fieldset id="basic-information" class="ui-widget">' . PHP_EOL; 
    $html .= '<legend>Basic Ballpark Information</legend>' . PHP_EOL; 
    $rowClass = "input-row"; 

    //$html .= $this->wrapLabelTextbox($this->inputBookingRef($values['max_booking_ref)']), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputBookingRef($max), $rowClass); 
    //$html .= $this->wrapLabelTextbox($this->inputBookingRef(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputBank(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputRegion(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputDescription(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputNotes(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputStartDate(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputRequestedDeliveryDate(), $rowClass); #$this->inputEndDate() 
    $html .= $this->wrapLabelTextbox($this->inputExpiryDate(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputURL(), $rowClass); 
    $html .= $this->wrapLabelTextbox($this->inputCAR(), $rowClass); 
    (strcmp($_GET["page"], "createballpark"))? 
     $html .= $this->wrapLabelTextbox($this->inputProjectStatusEdit(), $rowClass) : 
     $html .= $this->wrapLabelTextbox($this->inputProjectStatusCreate(), $rowClass);   
    $html .= '</fieldset>'; 
    return $html; 
} 

和我inputBookingRef功能:

private function inputBookingRef($values){ 
    //$values = $this->bookingRef; 
    $html = ""; 
    $values++; 
    $html .= '<label for="ref">Booking Ref: </label>'; 
    $html .= HTML::inputText("ref", 20, $values) . PHP_EOL; 
    return $html; 
} 

在這一行:

echo print_r($values, true); 

它顯示最後的最大值。但在我的文本框中,它只是給出了值1。我不知道我的編碼有什麼問題。任何幫助非常感謝,所以我可以繼續我的計劃。謝謝。

回答

0

每次調用

mysql_fetch_assoc($query) 

它會給你的結果的下一個值。 查詢中只有1行(如果這是真實的)。 行值分配給$值 並且WHILE循環根本不應該執行。

只留下語句之一,它應該是罰款;)註釋後

編輯: 還什麼是 $values++;inputBookingRef()方法的目的

$值可能是字符串類型.. ++會使它變得有趣。

+0

嗨註釋掉我的while循環。我已經嘗試過這一遍了,但仍然是文本框給我的價值數字1. –

+0

是的我在inputBookingRef函數中刪除了$ values ++,但是當我這樣做時,文本框中沒有值 –