php - 只能获取 $row ['id' 的一个值]

标签 php mysql database row record

我正在尝试获取数据库中的所有记录并且成功,但是我只能获取 1 个 id 值。我需要获取数据库中的所有 id 并调用 JavaScript 函数。在我的代码中,我只能获取最后一条记录的 id。我怎样才能得到它们?

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0; 
$message= $row['desc'];
$post = substr($message,$position,300); 
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */
echo "<form action='' method='post'  onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr;  //This should change and increment the value of id, isn't it?

echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?> 

这里发生的事情是,它将显示数据库中的记录:$row['header']、$row['date']、$row['desc'],然后是一个循环显示“阅读更多”的按钮。我想要发生的是在单击“阅读更多”后根据 id 调用一个函数。

例如,

___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 3
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 2
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 1
___________________________

这可能吗?或者我错过了什么。我的代码仅显示所有按钮的 $id=1 。对此我深表歉意。

最佳答案

you may try like this
make the id field name as array like more[] and bring the form out side loop as shown 
no need of for loop there to get the ids u already using while loop
bring submit button also outside loop

如果你想显示该帖子的详细信息,那么为什么你再次使用表单你可以简单地将其链接到详细信息页面 n 可以传递 ID,无需将其作为表单提交。

检查下面的代码

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";

$result = mysql_query($query);

//echo "<form action='' method='post'  onSubmit='ReadMore()'>";
echo "<table>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0; 
$message= $row['desc'];
$post = substr($message,$position,300); 
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */

//for($ctr=1;$ctr<=3;$ctr++){
//$id=$ctr;  //This should change and increment the value of id, isn't it?

//echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
//}
 echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";

</td></tr>";
echo "</tr>";
}

echo "</table>";
//echo "</form>";
mysql_close($con);
?> 

关于php - 只能获取 $row ['id' 的一个值],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707434/

相关文章:

java - 如何检查数据库中是否存在表或列?

java - 如何正确捕获 MySQLIntegrityConstraintViolationException?

mysql - 建议 SQL 查询从邻接列表模型中检索所有叶节点

php - WordPress 主题未正确显示图像

php - 在 WordPress 中为标签创建一个复选框

mysql - 如何两次加入同一张表

sql - 创建多个连接表的 View 是否比直接使用查询中的连接直接获取该数据更快?

java - 在 Java 中,每个查询都应该在一个单独的线程中吗?

php - 从列数不同的两个表中选择 *

php - 获取具有特定值的最新数据库条目