1

我正在编写一个带有 mysql 集成的简单 php 应用程序。
我有一个简单的 html 页面,我在其中连接数据库,然后为了验证,我想打印主机信息。

在 html 文件中有这个。
输出是两个水平条,它们之间没有任何东西。

代码有什么问题?

我在 aptana 工作室(也使用灯泡)的 ubuntu 12.10 上写作。

4

3 回答 3

0

你对你的 MySQLi 对象有点偏离,它应该更像..

$link = new mysqli("localhost", "my_user", "my_password", "dbname");
printf("Host Information: %s\n", $link->host_info);

MySQLi 是一个面向对象的系统,host_info 是一个公共成员,一旦你拥有一个有效的 MySQLi 实例,你就可以访问它。

http://php.net/manual/en/mysqli.get-host-info.php

编辑:允许错误报告并检查连接。

<?php
    error_reporting(E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Database Management Systems II - 2nd assignment</title>
    </head>
    <body>
        <hr>
        <?php
            $link = mysqli_connect("host","username","pass","database");
            if(mysqli_connect_error())
                echo "MySQLi Error: " . mysqli_connect_error();
            else
                echo "MySQLi Host Info: " . mysqli_get_host_info($link);
        ?>     
        <hr>
    </body>
</html>
于 2012-12-29T00:11:40.527 回答
0

检查以确保您实际连接到数据库:

$link = new mysqli("localhost", "my_user", "my_password", "dbname");
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}
于 2012-12-29T00:27:22.420 回答
0

所有迹象都表明您的 PHP 没有被执行期间。您的数据库代码可能仍然关闭,但我们甚至还没有到那个时候。这意味着正在发生两件事之一。您的扩展程序错误,或者您的 PHP 未配置。

首先,试试这个脚本(另存为 info.php):

<?php
    phpinfo();
?>

如果这不起作用,那么您没有配置。如果这确实有效,那么试试这个稍长的准系统示例;这是你的代码,没有数据库连接。您应该在两行之间看到一条存根消息。同样,保存时不要忘记 .php 扩展名:

<!DOCTYPE html>
<html>
    <head>
        <title>Database Management Systems II - 2nd assignment</title>
    </head>
    <body>
        <hr>
        <?php
            $link = 'link';
            printf("Host information: %s\n", $link);
        ?>
        <hr>
    </body>
</html>
于 2012-12-29T00:30:56.193 回答