PHP MySQL查询返回空

时间:2014-02-28 03:09:50

标签: php mysql mysqli

我正在尝试创建一个基本的PHP / MySQL测试脚本。当我尝试在我创建的数据库中打印两个表时(' andrew'),PHP输出返回none。但是,当我通过My​​SQL 5.6命令行运行相同的命令(' show tables;')时,将返回两个表。知道我做错了吗?

<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);

if(mysqli_connect_errno())
{echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else{echo 'Connected to MySQL! </ br>';}
$result=mysqli_query($connect,'show tables;');

if(!$result){
echo 'Nope';}
else{echo '<p>Result!!!</p>';}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_assoc($result)){
print_r ($row);}
echo "</ul>";
?>

3 个答案:

答案 0 :(得分:0)

尝试在mysql命令行中运行此sql:

使用mysql; 将所有onrew。*授予'root'@'localhost',由'root password'标识;

然后再次尝试运行您的脚本。

答案 1 :(得分:0)

您拥有的脚本工作正常。

<强>详情:

$host='localhost';
$username='root';
$password='';
$database='test';

<强>输出

Connected to MySQL!

Result!!!

Tables in database:

    Array ( [Tables_in_test] => tester ) 

答案 2 :(得分:0)

<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);
mysqli_select_db($connect,$database);

if(mysqli_connect_errno()) {
    echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else {
    echo 'Connected to MySQL! </ br>';
}
$result=mysqli_query($connect,'your sql query');

if(!$result){
    echo 'Nope';
} else{
    echo '<p>Result!!!</p>';
}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_array($result)){
print_r($row);
}
echo "</ul>";
?>