2016-12-14 107 views
0

的結果我有此示例代碼:獲取聲明

$db = new mysqli("localhost","root","password","xxx"); 

$statement = $db->prepare("SELECT name, password FROM persons WHERE name=? LIMIT 1"); 

$statement->bind_param('s', "kevin"); 

$statement->execute(); 

if($statement->num_rows){ 

$statement->bind_result($dbname, $dbpassword); 

$statement->free_result(); 

}; 

echo $dbname; 
echo $dbpass; 

如何使用/ GET $ dbname和$ DBPASSWORD上,無需使用類似:

while($statement->fetch()){ 
    echo $dbname; 
} 

我想用$ DBNAME和dbpassword直接。 什麼是最好的方法?我究竟做錯了什麼。

+2

將'$ num_rows'改爲'num_rows' – SuperDJ

+0

@SuperDJ事實上,這是一個錯誤,謝謝。 – Nomistake

+0

但是如何獲得和使用單行結果。也許fetch_row什麼的? – Nomistake

回答

1

我找到了答案:

我需要存儲結果(結果存儲(獲得性))

我需要獲取結果(取從準備好的語句結果的綁定變量)很高興沒有一個while循環。

$db = new mysqli("localhost","root","password","xxx"); 

$statement = $db->prepare("SELECT name, password FROM persons WHERE name=? LIMIT 1"); 

$statement->bind_param('s', "kevin"); 

$statement->execute(); 

$statement->store_result(); // this is what I was missing 

if($statement->num_rows){ 

    $statement->bind_result($dbname, $dbpassword); 

    $statement->fetch(); // this is what I was missing 

    $statement->free_result(); 

    echo $dbname; 

    echo $dbpass; 

};