如何在mysql_query中添加变量

时间:2014-03-28 06:07:50

标签: php mysql

我想在$query_bookname之后添加$query_author$_GET['category'] 我怎样才能做到这一点? (双引号和单引号让我很困惑)

$query_bookname = "AND book_name LIKE '%".$_GET['book_name']."%' ";
$query_author = "AND author LIKE '%".$_GET['author']."%' ";

$result = mysql_query("
    SELECT * FROM book
    WHERE cate_name = '".$_GET['category']."' AND status = 1
");

6 个答案:

答案 0 :(得分:1)

你想这样做,但是你需要解决代码中的弱点(根据Shankar Damodaran):

$result = mysql_query("
SELECT * FROM book
WHERE cate_name = '".$_GET['category']."' AND status = 1
$query_bookname
$query_author
");

答案 1 :(得分:0)

试试这个

$result = mysql_query("
    SELECT * FROM book
    WHERE cate_name = '".$_GET['category']." ".$query_author." ".$query_author." AND status = 1
");

答案 2 :(得分:0)

试试这个

$result = mysql_query("
    SELECT * FROM book
    WHERE cate_name = '".$_GET['category']."' AND status = 1
    $query_bookname 
    $query_author
");

答案 3 :(得分:0)

你可以这样试试

$result = mysql_query("SELECT * FROM book
  WHERE cate_name = '".$_GET['category']."' AND ".$query_bookname.$query_bookname." status = 1");

答案 4 :(得分:0)

PHP代码:

$q="";
$q .= " AND book_name LIKE '%".$_GET['book_name']."%' ";
$q .= " AND author LIKE '%".$_GET['author']."%' ";
$result = mysql_query("SELECT * FROM book WHERE cate_name = '".$_GET['category']."' AND status = 1 $q");

答案 5 :(得分:-1)

$query_bookname = ''; $query_author = '';
if(isset($_GET['book_name']) && $_GET['book_name']!=''){
   $query_bookname = " AND book_name LIKE '%".$_GET['book_name']."%' ";
}
if(isset($_GET['author']) && $_GET['author']!=''){
$query_author = " AND author LIKE '%".$_GET['author']."%' ";
}
$result = mysql_query("SELECT * FROM book   WHERE cate_name = '".$_GET['category']."'  $query_bookname  $query_author AND status = 1");