0

试图访问我机器上的 MySQL 数据库。它位于端口 1338 上。

这是 PHP 文件:

<!DOCTYPE html>

<html>

<head>

</head>


<body>

    <table>
    <?php

        $connection = mysql_connect("localhost:1338","root","password"); //connects to db
        $selected = mysql_select_db("site_updatelog"); //selects the db to be used
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }

        $sql = "SELECT comments, version, datetime FROM changeLog ORDER BY id DESC"; //form the query to be executed on the db
        $result = mysql_query($sql, $connections) or die (mysql_error()); //execute the query, or die if there was an error

        while($row = mysql_fetch_array($result)){
            //Display the table of results
            echo "<tr><td>". $row['version'] ."</td><td>". $row['comments'] ."</td><td>". $row['datetime'] ."</td></tr>";
        }

        mysql_close($connection);
     ?>
</table>


</body>

</html>

这是输出:

"; } mysql_close($connection); ?>
". $row['version'] ."   ". $row['comments'] ."  ". $row['datetime'] ."

折腾了好久还是不行。有任何想法吗?

4

3 回答 3

1

改变mysql_query($sql, $connections)_mysql_query($sql, $connection)

于 2012-11-24T14:49:26.073 回答
1

可能是因为您在回显的数据中有未转义的字符 - 尝试将您的 $row['comments']、$row['version'] 和 $row['datetime'] 包装到htmlspecialschars

于 2012-11-24T15:15:59.667 回答
0

使用下面的代码只会在以下情况下死亡并报告错误(!$connection)

$connection = mysql_connect("localhost:1338","root","password"); //connects to db
        $selected = mysql_select_db("site_updatelog"); //selects the db to be used
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }

尝试使用

$connection = mysql_connect("localhost:1338","root","password"); //connects to db
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }
        $selected = mysql_select_db("site_updatelog",$connection); //selects the db to be used
        if (!$db_selected) {
            die ("Can\'t use db : " . mysql_error());
        }
        $sql = "SELECT comments, version, datetime FROM changeLog ORDER BY id DESC"; //form the query to be executed on the db
        $result = mysql_query($sql) ;

        while($row = mysql_fetch_assoc($result)){
            //Display the table of results
            echo "<tr><td>". $row['version'] ."</td><td>". $row['comments'] ."</td><td>". $row['datetime'] ."</td></tr>";
        }

        mysql_close($connection);

这会在运行查询之前引发错误。也改为mysql_fetch_array()asmysql_fetch_assoc()需要mysql_fetch_array()2 个参数

于 2012-11-24T17:44:03.937 回答