[英]PHP code not returning correct MySQL result
我正在從托管服務器上的MySQL查詢一組數據。 當我在本地計算機上執行此操作時,它工作正常,但在托管服務器上,它不返回任何內容。 奇怪的是,當我在托管環境中使用此GUI來運行相同的查詢時,它確實會返回結果。
所以這是一些代碼,等等。
這是index.php:
<?php
$servername = "username.db.theservername.ether.com";
$mysqllogin = "username";
$mysqlpassword = "thepassword";
$dbName = "StepsMath";
$conn = new mysqli($servername, $mysqllogin, $mysqlpassword, $dbName);
if($conn->connect_error){
die ("connection failed: " . $conn->connect_error);
echo "connection failed.";
}
$set_name = 'test_set';
$query = "select word, definition from word_list where set_name = '$set_name'";
$result = $conn->query($query);
$conn->close();
// *********************************************
echo $query; // <-------- *******this prints the query*******
// *********************************************
$result = mysqli_fetch_array($result);
?>
<!DOCTYPE html>
<html lang="en"><head>
<title>this is a title</title>
<script type="text/javascript">
var json = '<?= json_encode($result) ?>';
var word_list = JSON.parse(json);
console.log(word_list); //line 24
console.log(json); //line 25
function getUserId(){
return '<?= $user_id ?>';
}
if(!getUserId()) window.location = 'login.html';
</script>
</head>
<body>body stuff</body>
</html>
表word_list僅具有三列:word,definition和set_name
上面的代碼應該返回單詞和定義的行。 相反,當我在瀏覽器控制台中簽入時,它將返回以下內容:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
這是運行的查詢:
select word, definition from word_list where set_name = 'test_set'
如果我復制該確切查詢並在托管服務器的MySQL GUI中運行它,
返回以下內容:
因此,總結一下問題,為什么GUI和代碼之間的結果會有差異?
更改此行:
$result = mysqli_fetch_array($result);
對此:
while ($row = mysqli_fetch_assoc($result)) {
$res[] = $row;
}
var_dump($res);
您先前獲得的信息: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
mysqli_result
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
都是mysqli_result
屬性。
閱讀此書,它將幫助您了解自己做錯了什么: http ://php.net/manual/en/class.mysqli-result.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.