MySQL查询在PHP中返回null但在终端中很好

问题描述:

I'm new to both PHP and MySQL so it is probably an easy fix but I cannot figure it out. 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.

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

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

Thanks in advanced for any help.

我是PHP和MySQL的新手,所以它可能是一个简单的修复,但我无法弄清楚。 当我向终端输入一个查询它返回我想要的绝对正常,但当我使用PHP发送查询时,它返回一个空数组,结果中也没有任何内容。 p> 这是我发送查询然后将其添加到另一个数组的代码。 strong> p>

  include'mysql.php'; 
 $ sql  =“SELECT username FROM members”; 
 $ result = $ conn-> query($ sql); 
 
 $ return = array(); 
 
while($ row = mysqli_fetch_array($ results)){  
 $ return [] = $ row ['username']; 
} 
  code>  pre> 
 
 

mysql.php strong> p> \ n

  $ conn = new mysqli($ servername,$ username,$ password,$ dbname); 
if($ conn-> connect_error){
 die(“Connection failed:”。$  conn-> connect_error); 
} 
  code>  pre> 
 
 

感谢您提前获取任何帮助。 p> div>

You are using $results instead of $result Change it to

$result = $conn->query($sql);

$return = array();

while($row = mysqli_fetch_array($result)){
$return[] = $row['username'];
}