mysql搜索结果问题

时间:2011-09-16 13:50:09

标签: php mysql search select

一切都假装工作得很好,除非我搜索任何东西,我得不到没有错误的结果。我认为我的问题在我的询问范围内,但我似乎可以指出它。我之前做了一些搜索,但只链接了2个表。现在我正在尝试链接4个表,它变得相当复杂。我想澄清一下我可能需要调整什么以清除这个混乱。

    <?php


$search = $_GET['search'];


if (!$search) 
echo "You didn't enter a keyword";
else
{
  echo "<td>You searched for: <strong>$search </strong></td><br><br>";
  mysql_connect('localhost','myuserexample','mypassexample');
  mysql_select_db('spusers');  
  $id=@$_GET['id'];


  $query="
        SELECT
            spusername.id, spusername.firstname, spusername.lastname, spusername.splocation_id,
            sptraining.id, sptraining.trainingtype, sptraining.level,
            splocation.id, splocation.location,
            sprecord.spusername_id, sprecord.sptraining_id

        FROM spusername 
        JOIN sprecord ON spusername.id = sprecord.spusername_id
        JOIN sptraining ON sprecord.sptraining_id = sptraining.trainingtype
        JOIN splocation ON spusername.splocation_id = splocation.location
        WHERE MATCH ( firstname, lastname, trainingtype, level, location, spusername_id, sptraining_id, splocation_id ) 
        AGAINST('%".$search."%' IN BOOLEAN MODE) ORDER BY lastname ASC";

  $result1 = MySQL_query($query);  
  if(!$result1) {  
    echo MySQL_error()."<br>$query<br>";
  }  
  if (MySQL_num_rows($result1) > 0) {
    echo "<table class='sortable' width='750' align='center' border='1' bordercolor='#000000' bgcolor='#000000'
           cellspacing='2' cellpadding='2'><tr><th bgcolor=#999999>
           Employee</th><th bgcolor=#999999>
           Location</th><th bgcolor=#999999>
           Training</th><th bgcolor=#999999>
           Level</a></th><th bgcolor=#999999>
           Date Completed</th></tr bgcolor=#999999>";
    while($result2 = MySQL_fetch_array($result1)) {


      echo  "<td bgcolor=#d4d5ff>{$result2['lastname']},
            {$result2['firstname']}</td><td bgcolor=#d4d5ff>
            {$result2['location']}</td><td bgcolor=#d4d5ff>
            {$result2['trainingtype']}</td><td bgcolor=#d4d5ff>
            {$result2['level']}</td></tr>";
    }
    echo "</table>";
  } else {
    echo "No Results were found in this category.<br>";
  } 
  echo "<br>";
}
?>

1 个答案:

答案 0 :(得分:0)

...解决

而不是

JOIN sptraining ON sprecord.sptraining_id = sptraining.trainingtype JOIN拆分ON spusername.splocation_id = splocation.location

我错误地说它等于训练类型和位置而不是id。

工作示例: 在sprecord.sptraining_id = sptraining.id上加入sptraining JOIN拆分ON spusername.splocation_id = splocation.id