我们想要显示数据库'X'中一个表的列表链接。 这是我的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "root";
$password = "mysql";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("nentholbenin", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM utilisateurs ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Categories</th>
</tr>
</thead>
<tbody>
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
foreach($array as $index => $db_result){
echo '<li><a href=".'$db_result['Categories'].'"</a></li>';
}
echo '</ul>';
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
我收到此错误:
解析错误:语法错误,意外'$ db_result'(T_VARIABLE), 期待','或';'在
答案 0 :(得分:1)
试试这个:
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
foreach($array as $index => $db_result){
echo '<li><a href="'.$db_result['Categories'].'"</a></li>';
}
echo '</ul>';
?>
答案 1 :(得分:0)
你在$ db_result之前忘了点。 使用mysql_query执行查询后,您无法直接获取数据。使用mysql_fetch_array或mysql_fetch_assoc
尝试以下代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "root";
$password = "mysql";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("nentholbenin", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM utilisateurs ");
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>Categories</th>
</tr>
</thead>
<tbody>
<?php
$db_result = mysql_query("SELECT Categories FROM utilisateurs");
$result = $db_result; echo '<ul>';
while($array = mysql_fetch_assoc($result)) {
echo '<li><a
href="/'.$array['Categories'].'">'.$array['Categories'].'</a></li>';
}
echo '</ul>';
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>