用搜索形式的PHP查询MySQL查询问题

时间:2015-01-28 19:39:47

标签: php mysql get

我编写了一个基本的PHP站点,它将参数从一个页面传递到另一个页面并查询mysql数据库。

我已检查过该参数已通过OK,但查询中的where参数似乎无法正常工作。有人可以帮忙吗?

该网站是www.retroseek.co.uk,搜索框位于底部。非常感谢。

<!DOCTYPE html>

<html>

<head>

<title>Retro Computing Resource - RetroSeek Search</title>

<meta charset="UTF-8">


<style>
table, td, th {
    border: 1px solid green;
}

th {
    background-color: green;
    color: white;
}
</style>


</head>

<body>

<h1>Results from the RetroSeek Search Engine</h1>

<?php 

echo "<h1>Testing the GET has worked " . $_GET["query"] . "</h1>";


require( '../connect_db.php' ) ;

$query = $_GET['query']; 


$query = htmlspecialchars($query); 


$query = mysql_real_escape_string($query);

echo "<h1>Testing the GET has worked " . $_GET["query"] . "</h1>";



    $min_length = 4;
    // you can set minimum length of the query if you want


$q = "SELECT fgames.fgname, fgames.fgprorg, fgames.fgprbud, fgames.fgratng, fgames.fgprdsk, ftypes.fttname, frefs.fryymm, frefs.frpage, frefs.frissue, fmagzne.fmname, frefcde.ffname
FROM fgames, ftypes,frefs, fmagzne,frefcde
WHERE ftypes.fttype = fgames.fgtype
and   fgames.fglink = frefs.frlink2
and   frefs.frentry = frefcde.ffentry
and   frefs.frmag = fmagzne.fmmag 
and   fgames.fgname LIKE '%".$query."%'" ;



$r = mysqli_query( $dbc , $q ) ;

if( $r ) 
{


  echo '<table><tr><th>FGNAME</th><th>FGPRORG</th><th>FGRBUD</th><th>FGRATNG</th><th>FGRDSK</th><th>FTTTNAME</th><th>FRYYMM</th><th>FRPAGE</th><th>FRISSUE</th><th>FMNAME</th><th>FFNAME</th></tr>';

  while ( $row = mysqli_fetch_array( $r , MYSQLI_ASSOC ) ) 
  {
    echo '<tr><td>'.$row['fgname'].'</td><td>'.$row['fgprorg'].'</td><td> '.$row['fgprbud'].' </td><td>'.$row['fgratng'].' </td><td>'.$row['fgprdsk'].' </td><td>'.$row['fttname'].'</td><td> '.$row['fryymm'].'</td><td> '.$row['frpage'].'</td><td> '.$row['frissue'].' </td><td>'.$row['fmname'].' </td><td>'.$row['ffname'].'</td></tr>';
  }
  echo '</table>';
}

else
{
  echo '<p>' . mysqli_error( $dbc ) . '</p>'  ;
}

show_records($dbc);

# Close the connection.
mysqli_close( $dbc ) ;
?>



<h3>Contact me at jonesypeter@hotmail.com if you would like any information on my plans for this website. Please send me as much feedback and suggestions as possible.</h3>
<h4>Last updated on 28 January 2015</h4>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

如果参数中有特殊字符或空格,有时使用$ _GET会有点困难。

我看到你对$ _GET ['query']进行了处理,但如果你有空格,你可以得到不同的结果。

请回显$ q并尝试在phpMyAdmin或mysql终端中运行结果。 如果它工作并返回您想要的数据,请将其放在代码的最顶层:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

并报告错误。

顺便说一下,mysqli_error()中没有错误?

答案 1 :(得分:0)

仅供参考,就我个人而言,我觉得这更容易阅读:

"
SELECT g.fgname
     , g.fgprorg
     , g.fgprbud
     , g.fgratng
     , g.fgprdsk
     , t.fttname
     , r.fryymm
     , r.frpage
     , r.frissue
     , m.fmname
     , frefcde.ffname
  FROM fgames g
  JOIN ftypes t
    ON t.fttype = g.fgtype
  JOIN frefs r
    ON r.frlink2 = g.fglink 
  JOIN fmagzne m
    ON m.fmmag = r.frmag 
  JOIN frefcde
    ON frefcde.ffentry = r.frentry 
 WHERE g.fgname LIKE '$query';
";