简体   繁体   English

无法连接到MySQL /其他错误。 使用PHP

[英]Cannot Connect To MySQL / Other Error. Using PHP

Trying to access a MySQL database on my machine. 试图访问我的机器上的MySQL数据库。 It's on port 1338. 它在1338端口。

This is the PHP file: 这是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>

This is the output: 这是输出:

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

Been messing around for quite a while and it still wont work. 已经搞乱了很长一段时间,它仍然无法正常工作。 Any ideas? 有任何想法吗?

使用mysql_query($sql, $connections)更改mysql_query($sql, $connections) mysql_query($sql, $connection)

可能是因为你的数据中有非转义字符,你正在回应 - 尝试将你的$ row ['comments'],$ row ['version']和$ row ['datetime']包装到htmlspecialschars

Using the code below only dies and reports error if (!$connection) 使用下面的代码只会死亡并报告错误if (!$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
        }

Try using 尝试使用

$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);

This throws errors before running query. 这会在运行查询之前抛出错误。 Also changed mysql_fetch_array() to mysql_fetch_assoc() as mysql_fetch_array() requires 2 parameters mysql_fetch_array()更改为mysql_fetch_assoc()因为mysql_fetch_array()需要2个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM