[英]How would I display content from a different mysql row in php?
I'm sorry if the title might confuse you, it will make more sense with some context. 很抱歉,标题可能会使您迷惑,在某些情况下,它会更有意义。
In my mysql table I have rows all sorted by id and they have a place for the title, content and date. 在我的mysql表中,所有行均按ID排序,并且它们的标题,内容和日期都有位置。 In my php code I display the title to the post, using "echo $title" which works fine.
在我的PHP代码中,我使用“ echo $ title”显示帖子的标题,效果很好。 But on the same page I want to display the title to my 2nd row but when I use echo $title it shows the same title as the first row.
但是在同一页面上,我想显示第二行的标题,但是当我使用echo $ title时,它显示的标题与第一行相同。
If you need any more information I'd be glad to help. 如果您需要更多信息,我们将很乐意为您提供帮助。
To iterate over an array you need to use fetch the query as an associative array then loop over it. 要遍历数组,您需要使用将查询作为关联数组使用,然后对其进行循环。 This should let you control the values and parse them as you want them.
这应该使您可以控制值并根据需要解析它们。
http://us3.php.net/mysql_fetch_assoc http://us3.php.net/mysql_fetch_assoc
$queryData = mysql_fetch_assoc($query);
$title1 = $queryData[0]['title'];
$title2 = $queryData[1]['title'];
Have fun learning and exploring, PHP is a field of fun 有趣的学习和探索,PHP是一个有趣的领域
The problem is not clear that much. 问题不是很清楚。 But maybe, you want to show your all SQL data in a page.
但是也许您想在页面中显示所有SQL数据。 You already got first row's first data but want to seconds rows data?
您已经获得了第一行的第一数据,但想秒数据? or all data?
还是所有数据? If it is then you can just apply a loop to show the all data in a table.
如果是,那么您可以应用循环以显示表中的所有数据。 Here is the simple mysqli procedural PHP code.
这是简单的mysqli程序PHP代码。 You can also use PDO or Object oriented approach PHP code.
您还可以使用PDO或面向对象的方法PHP代码。
In show.php file 在show.php文件中
/*make database connection here. You have already did it.
I assume that your connection variable name is $conn;*/
/*SQL query for show all data in tha *allposts* table */
$sql_query = "SELECT * FROM allposts";
$result = $conn->query($sql_query);
<table>
<tr>
<th>Title</th>
<th>Content</th>
<th>Date</th>
</tr>
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
<tr>
<td>echo $row["title"];</td>
<td>echo $row["content"];</td>
<td>echo $row["date"]</td>
</tr>
}
} else {
echo "0 results";
}
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.