[英]MySQL query is returning null in PHP but is fine in terminal
I'm new to both PHP and MySQL so it is probably an easy fix but I cannot figure it out. 我是PHP和MySQL的新手,所以它可能很容易解决,但我无法弄清楚。 When I put in a query into terminal it returns what I want absolutely fine, but when I used PHP to send the query, it returns an empty array and the result has nothing in it as well.
当我在终端中输入查询时,它会返回我想要的结果,但是当我使用PHP发送查询时,它返回一个空数组,并且结果中也没有任何内容。
This is my code for sending the query and then adding it to another array. 这是我的代码,用于发送查询,然后将其添加到另一个数组。
include 'mysql.php';
$sql = "SELECT username FROM members";
$result = $conn->query($sql);
$return = array();
while($row = mysqli_fetch_array($results)){
$return[] = $row['username'];
}
mysql.php mysql.php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Thanks in advanced for any help. 非常感谢您的任何帮助。
You are using $results
instead of $result
Change it to 您使用的是
$results
而不是$result
更改为
$result = $conn->query($sql);
$return = array();
while($row = mysqli_fetch_array($result)){
$return[] = $row['username'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.