我从我的数据库中查询我正在处理的论坛。由于某种原因,我无法获得链接中显示的ID。我一直在试图解决它的问题。除了这个之外,它适用于我的所有其他页面。这是我正在使用的代码:
$topicsql = "SELECT topic_id,topic_subject,topic_date,topic_cat FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC LIMIT 1";
$topicsresult = mysqli_query($con, $topicsql);
if(!$topicsresult)
{
echo 'Last topic could not be displayed.';
}
else
{
if(mysqli_num_rows($topicsresult) == 0)
{
echo 'no topics yet';
}
else
{
while($topicrow = mysqli_fetch_assoc($topicsresult))
//Limit the number of characters in the latest topic link
$subject= substr($topicrow['topic_subject'], 0, 25);
$topic=$topicrow['id'];
echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $subject . '…</a><br> on ' . date('m-d-Y', strtotime($topicrow['topic_date']));
}
}
当你将鼠标悬停在链接上时我会得到topic.php?id = ......我应该看到topic.php?id = 24
你能帮帮我吗?
答案 0 :(得分:0)
尝试这种方法:
$query = "SELECT topic_id,topic_subject,topic_date,topic_cat FROM topics
WHERE topic_cat = ?
ORDER BY topic_date DESC LIMIT 1";
if ($stmt = mysqli_prepare($con, $query)) {
mysqli_stmt_bind_param($stmt,'i', $row['cat_id']);
/* execute prepared statement */
mysqli_stmt_execute($stmt);
/*bind the result*/
$stmt->store_result();
/*Count the rows*/
if( mysqli_stmt_num_rows($stmt) > 0){
while($row = mysqli_fetch_assoc($stmt)){
/*Limit the number of characters in the latest topic link*/
$link = '<a href="topic.php?id=' . $row['topic_id'] . '">';
$link .= substr($row['topic_subject'], 0, 25) . '…</a><br>';
$link .= 'on '.date('m-d-Y', strtotime($row['topic_date']));
echo $link;
}
}else{
echo 'Last topic could not be displayed.';
}
/* close statement */
mysqli_stmt_close($stmt);
}else{
printf("Errormessage: %s\n", mysqli_error($con));
}
由于它正在创建链接,这意味着您的查询找到了行,如果您看到id为空,则表明列名称不匹配。
此时只有你可以说,我的建议是重写上面的代码,我只是想指出准备好的语句,命名变量,减少数量变量,缩进,吃蔬菜的最佳实践......所有这些都将帮助您避免错误,就像这个令人困惑的代码部分一样:
首先查询:
SELECT topic_id ...
然后当你检索到:
$topic=$topicrow['id'];
打印查询:
echo '<a href="topic.php?id=' . $topicrow['topic_id']
所以请仔细阅读您的代码并注意细节。