简体   繁体   English

PHP代码未返回正确的MySQL结果

[英]PHP code not returning correct MySQL result

I'm querying a set of data from MySQL on a hosted server. 我正在从托管服务器上的MySQL查询一组数据。 When I did it on my local machine, it worked fine, but on the hosted server, it's not returning anything. 当我在本地计算机上执行此操作时,它工作正常,但在托管服务器上,它不返回任何内容。 The weird thing is, when I use this GUI for the hosted environment to run the same query, it does return the results. 奇怪的是,当我在托管环境中使用此GUI来运行相同的查询时,它确实会返回结果。

So here are some codes, etc. 所以这是一些代码,等等。

This is index.php: 这是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>

Table word_list only has three columns: word, definition, and set_name 表word_list仅具有三列:word,definition和set_name

The above code is supposed to return rows of words and definitions. 上面的代码应该返回单词和定义的行。 Instead, as I check in the browser console, it returns the following: 相反,当我在浏览器控制台中签入时,它将返回以下内容:

{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

This is the query that runs: 这是运行的查询:

select word, definition from word_list where set_name = 'test_set'

If I copy that exact query and run it in the MySQL GUI in the hosted server, 如果我复制该确切查询并在托管服务器的MySQL GUI中运行它,

GoDaddy提供的GUI MySQL环境

the following is returned: 返回以下内容:

在GUI中运行相同查询的结果

So to summarize the question, why is there a discrepancy in the result between the GUI and the code?? 因此,总结一下问题,为什么GUI和代码之间的结果会有差异?

change this line: 更改此行:

$result = mysqli_fetch_array($result);

to this: 对此:

while ($row = mysqli_fetch_assoc($result)) {
    $res[] = $row;
}
var_dump($res);

The information you previously got: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null} are all mysqli_result properties. 您先前获得的信息: {"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属性。

Read this, it will help you uderstand what you did wrong: http://php.net/manual/en/class.mysqli-result.php 阅读此书,它将帮助您了解自己做错了什么: http ://php.net/manual/en/class.mysqli-result.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM