2013-04-18 77 views
-1

誰能給我一個建議?爲什麼查詢不能提供給我預期的價值?謝謝。Mysql準備語句 - 請選擇

$mysqli = new mysqli($GLOBALS["mysql_host"], $GLOBALS["mysql_user"],   $GLOBALS["mysql_passwd"], $GLOBALS["mysql_database"]); 
$stmt = $mysqli->prepare("SELECT one FROM table ORDER BY date DESC LIMIT 1"); 
$last = $stmt->bind_result($one); 
$stmt->execute(); 
$stmt->close(); 
$mysqli->close(); 

Echo $last; //it should be "abc" 
+0

-1:發表評論 –

+0

你的問題是「誰能給我建議?」。那麼,只有懂得你需要的人才可以。爲此,您的問題需要不太模糊。 – Cubetastic

回答

4

我認爲你必須execute,然後調用fetchmysql_stmt -objects。 因爲您可能會得到多個結果(行)。

隨着fetch你會提高你的結果 - 遊標。

文檔: mysqli mysqli-stmt

<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

/* prepare statement */ 
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { 
    $stmt->execute(); 

    /* bind variables to prepared statement */ 
    $stmt->bind_result($col1, $col2); 

    /* fetch values */ 
    while ($stmt->fetch()) { 
     printf("%s %s\n", $col1, $col2); 
    } 

    /* close statement */ 
    $stmt->close(); 
} 
/* close connection */ 
$mysqli->close(); 

?> 
0

我可以。
一個的建議是簡單明瞭的:不使用的mysqli

使用PDO代替

$stmt = $pdo->prepare("SELECT one FROM table ORDER BY date DESC LIMIT 1"); 
$stmt->execute(); 
$last = $stmt->fetchColumn(); 

echo $last; //it should be "abc" 

乾淨,簡單,工作

+1

MySQLi出了什麼問題? –

+0

確實,我更喜歡PDO。 它有一個很好的抽象層次。 在nettuts有一個[很好的解釋](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/) –