数据库已连接但选择查询不起作用?

时间:2014-01-27 10:54:19

标签: php database

我的数据库在梦想主机上。它无法连接。  我的代码是

<?php
    $hostname = "mysql.demos.smartmobe.com";   // eg. mysql.yourdomain.com (unique)
    $username = "nayacinema";   // the username specified when setting-up the database
    $password = "****";   // the password specified when setting-up the database
    $database = "nayacinema";   // the database name chosen when setting-up the database (unique)



    $con=mysqli_connect($hostname,$username,$password,$databse);
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }else{
        echo 'done';
    }
    $result = mysqli_query($con,"SELECT * FROM TblUsers");
    print_r($result);

    while($row = mysqli_fetch_array($result))
    {
        echo $row['FirstName'] . " " . $row['LastName'];
        echo "<br>";
    }

?>

它给出了像这样的错误

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/demo_smartmobe/demos.smartmobe.com/nayacinema/test.php on line 20

可能是什么问题?

2 个答案:

答案 0 :(得分:4)

你有拼写错误。

$con=mysqli_connect($hostname,$username,$password,$databse);

应该是

$con=mysqli_connect($hostname,$username,$password,$database);

数据库拼写错误)

答案 1 :(得分:0)

我已经测试过您的代码没有连接..因为拼写错误这里是正确的代码,我已在我的本地机器上测试过

<?php
    $hostname = "localhost";   // eg. mysql.yourdomain.com (unique)
    $username = "root";   // the username specified when setting-up the database
    $password = "";   // the password specified when setting-up the database
    $database = "test";   // the database name chosen when setting-up the database (unique)



    $con=mysqli_connect($hostname,$username,$password,$database);
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }else{
        echo 'done';
    }
    $result = mysqli_query($con,"SELECT * FROM user");
    print_r($result);

    while($row = mysqli_fetch_array($result))
    {
        echo $row['id'] . " " . $row['user_name'];
        echo "<br>";
    }

?>

由于