我試圖爲應用程序項目設置API。php-無法從mysql獲取結果(mysqli)
我有一個名爲'users'的mysql表,我已經添加了一行。使用
代碼:
// Create connection
$mysqli = new mysqli("localhost","user", "pass", "db");
// Check connection
if($mysqli->connect_errno){
$result = "Failed to connect to MySQL: " . mysqli_connect_error();
print_r(json_encode($result));
return false;
}
$row = $mysqli->query("SELECT * FROM users");
print_r(json_encode($row));
我得到一個空的結果,怎麼來的? (連接不拋出一個錯誤)
是準確的,我得到:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
編輯:
得到了答案,原來YM的問題,謝謝!
所以現在使用的代碼:
$row = $mysqli->query("SELECT * FROM users WHERE email = '".$email."'");
$result = $row->fetch_array();
print_r(json_encode($result));
我得到的結果是:
{"0":"test","username":"test","1":"[email protected]","email":"[email protected]","2":"test","password":"test","3":"2013-10-18 22:22:53","date_registered":"2013-10-18 22:22:53","4":"1","id":"1"}
這裏我想是這樣的:
{"username":"test","password":"test","email":"[email protected]", ...etc }
如何獲得的?
如果您以前使用過'mysql_ *',您可以通過運行mysql_query()來獲得結果嗎?我不這麼認爲。從查詢中獲取數組 –
您是否需要從$ row中獲取結果? –
編輯我的答案。您需要使用[mysqli_fetch_assoc()](http://www.php.net/manual/en/mysqli-result.fetch-assoc.php)以避免使用數字索引重複結果。 –