[英]Cannot Connect To MySQL / Other Error. Using PHP
試圖訪問我的機器上的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'] ."
已經搞亂了很長一段時間,它仍然無法正常工作。 有任何想法嗎?
使用mysql_query($sql, $connections)
更改mysql_query($sql, $connections)
mysql_query($sql, $connection)
可能是因為你的數據中有非轉義字符,你正在回應 - 嘗試將你的$ row ['comments'],$ row ['version']和$ row ['datetime']包裝到htmlspecialschars
使用下面的代碼只會死亡並報告錯誤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
}
嘗試使用
$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()
更改為mysql_fetch_assoc()
因為mysql_fetch_array()
需要2個參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.