Trying to access a MySQL database on my machine. It's on port 1338.
This is the PHP file:
<!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)
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.