Php mySQL搜索没有真正起作用
这是一个mySQL查询,其中的密码和用户名被故意弄糟了。它只返回一个结果,不返回该结果今天日期以外的任何其他信息。无论尝试什么搜索,它都不起作用Php mySQL搜索没有真正起作用,php,mysql,search,Php,Mysql,Search,这是一个mySQL查询,其中的密码和用户名被故意弄糟了。它只返回一个结果,不返回该结果今天日期以外的任何其他信息。无论尝试什么搜索,它都不起作用 <?php $proto = $_GET['p']; $terms = $_GET['f']; $return; if($proto == 'inline'){ echo 'checking'; $username="*******"; $password="*******"
<?php
$proto = $_GET['p'];
$terms = $_GET['f'];
$return;
if($proto == 'inline'){
echo 'checking';
$username="*******";
$password="*******";
$database="*******";
$my_text = $_GET['f']; //what I'm searching for
$my_category = '8'; //whatever category number it is
mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$result = mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");
// select all posts that have your text
while ($row = mysql_fetch_array($result));
$postname = $row['post_name'];
$posttitle = $row['post_title'];
$postID = $row['ID'];
$date = date ( 'd-M-Y' , strtotime($row['post_date']) );
$return.= '
<a href="http://www.robin-knight.com/' . $postname .'">'.$posttitle.' ('.$postname.')<br /><span style="font-size:10px; color:#555;">'.get_the_time("d-M-Y", $postID).' - '.get_post_meta($postID, "status", true).'</span></a>
';
//while have posts
echo $return;
}
?>
您的查询没有选择post\u name
、post\u title
或post\u date
。你说的是从wp_帖子中选择ID,所以它按照指示选择ID而不是别的。您的date()
调用排序工作正常,因为strotime()
返回false,所以它使用今天的日期作为回退。这是:
mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%$my_text%' ");
是非常不安全的-my_text
不会被转义,并且是从GET参数中获取的,因此它会使您遭受SQL注入攻击。使用mysql\u real\u Escape\u string对其进行转义,或者更好地在代码中使用参数化查询。除了其他信息外:
我可能会错过它,但我看不到您在while循环中包含任何内容
这就是为什么它只得到一个。你需要用大括号{}来学习SQL是如何工作的,我想…你的问题是什么?还有,“没有真正工作”意味着什么?漂亮的SQL注入漏洞。希望你喜欢有人会停在你数据库中的卡车。这是对我问题的绝对精彩的回答。如果你需要post\u name
,你必须选择它从wp\u posts中选择ID、post\u name、post\u title、post\u date…
。我认为这可能是问题中的一个输入错误,但这是一个很好的观点。@Robin Knight——如果这是错误,您应该接受这个答案。