繁体   English   中英

php 回显来自特定 id 的数据

[英]php echo data from certain id

编辑:对不起,我不清楚,这次我试着解释一下。

我在名为 tMenu 的数据库表中有这些数据:

id page_nl 文本
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。我希望我的问题现在更清楚了。

您需要使用foreach($var as $key =>$value)循环

你的问题不是很清楚 - 你是在要求这样的吗

$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'];
    }
}

假设您的意思是数据库表,您需要一个例程来连接到数据库然后获取信息:

<?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();
?>

暂无
暂无

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

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