我在名为tMenu的数据库表中有这些数据:
id page_nl text
1 index_1 index1_text
2 index_2 index2_text
3 index_3 index3_text
这些是我网站上的3个页面,在本例中称为index_1,index_2和index_3。我编程它是这样一种方式,每个页面显示index1_text。
我现在想要的是在菜单中显示page_nl。我现在的代码是:
$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';
<a href="index_1.php"><? echo $row['page_nl']; $id=="1" ;?></a>
<a href="index_2.php"><? echo $row['page_nl']; $id=="2" ;?></a>
<a href="index_3.php"><? echo $row['page_nl'];?></a>
现在它的方式只显示来自id 1的page_nl,但我希望下一个链接显示来自id 2的page_nl。我希望我的问题现在更清楚了。
答案 0 :(得分:0)
您需要使用foreach($var as $key =>$value)
循环
答案 1 :(得分:0)
你的问题不是很清楚 - 你是否要求这样的事情
$sql = "select * from yourtable where id = 1";
$result = mysql_query($sql);
//I am assuming there are more than 1 rows for ID 1
while($row = mysql_fetch_assoc($result)) {
echo $row['page_nl'];
}
或============================
$sql = "select * from yourtable"; //Select All
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if($row['id'] == 1)
{
echo $row['page_nl'];
}
}
答案 2 :(得分:0)
假设您的意思是数据库表,您需要一个例程来连接数据库然后获取信息:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table_name"; // put table name here
$result = $mysqli->query($query);
/* numeric array */
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>