我无法使用数据库中的数据链接我的搜索结果
<?php
include 'connect.php';
error_reporting(0);
if(isset($_POST['search'])) {
$searchquery = $_POST['search'];
$searchquery = preg_replace("#[^0-9a-z]#i","",$searchquery);
$query = mysql_query("SELECT * FROM tbl_venues WHERE venue_name LIKE '%$searchquery%' OR venue_adress LIKE '%$searchquery%'") or die("Search failed!");
$count = mysql_num_rows($query);
if($count == 0){
$output = 'No results found!';
}else{
while($row = mysql_fetch_array($query))
{
$vname = $row['venue_name'];
$vadress = $row['venue_adress'];
$id = $row['venue_id'];
$output .='<div id="searched">'.$vname.'<a href="venue.php?venue=" . $row['venue_id']?>"</a></div>';
}
}
}
?>
上面是我的代码,我需要 field 的id作为具体页面示例
venue.php?venue=$id
我认为问题出在 $output 行
我可以正常执行此操作,但事实证明要获得搜索结果来执行此操作很困难。 谢谢
最佳答案
您滥用了'
和"
。当您学习 PHP 时,这两个引号分隔符是一个众所周知的难题。我认为您还将 field 名称字符串放在 <a>
之外标签。
如果我理解正确的话,你希望在输出中出现这种东西。
<div id="searched"><a href="venue.php?venue=7">Fillmore West</a></div>
如果我是你,我就会使用这个构造来完成这样的事情。
$output .= sprintf('<div id="searched"><a href="venue.php?venue=%d">%s</a></div>',
$row['venue_id'],$vname);
使用sprintf()
使您的字符串替换可见。
当您使用单引号 'string'
时,php 无法识别字符串中的任何内容。这适用于sprintf()
的第一个参数,因为它是在内部处理的。
关于php - 使用列数据链接搜索结果时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032444/