0

我有一个与 php 和内部加入一起使用的选择查询,但浏览器显示错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“SELECT specialization_name FROM user u INNER JOIN spe”附近使用正确的语法

谁能帮我 ?

这是查询:

if(isset($_POST['listbyq']))
{
  if($_POST['listbyq']=="newest_members")
  {
      $querySrting="WHERE registered_date!='' ORDER BY registered_date DESC " or die(mysql_error());
      $queryMSG="Showing senior to oldest memebrs";
  }
elseif($_POST['listbyq']=="by_specialization")

   {
      $querySrting="SELECT specialization_name FROM user u     INNER JOIN specialization s     ON u.specialization=s.specialization_id      WHERE u.user_id='$id'";

      $result = mysql_query($queryString)  or die(mysql_error());
      $queryMSG="showing members with specs";
   }
elseif($_POST['listbyq']=="by_firstname")
  {
      $fname = $_POST['fname'];
      $fname = stripcslashes($fname);
      $fname = strip_tags($fname);
      $querySrting="WHERE  first_name LIKE '%$fname%'"or die(mysql_error());
      $queryMSG="Showing member with the name you searched for";
  }
}
///******query the member data using the queryString*******//
$sql = mysql_query("SELECT user_id, first_name, last_name, birth_date, registered_date FROM user $querySrting") or die(mysql_error());
 //**********************outputlist*********************************//
 $outputlist="";
 while($row = mysql_fetch_array($sql))
 {
     $id=$row['user_id'];
     $firstname=$row['first_name'];
     $lastname=$row['last_name'];
     $birthdate=$row['birth_date'];
     $spec = $row['specialization'];
     $registereddate=$row['registered_date']; 
     ////***********for the upload image*************************//
      $check_pic="members/$id/image01.jpg";
   $default_pic="members/0/image01.jpg";
   if(file_exists($check_pic))
   {
       $user_pic="<img src=\"$check_pic\"width=\"120px\"/>";
   }
   else
   {
       $user_pic="<img src=\"$default_pic\"width=\"120px\"/>";
   }

   $outputlist.='
   <table width="100%">
               <tr>
                  <td width="23%" rowspan="3"><div style="height:120px;overflow:hidden;"><a href = "http://localhost/newadamKhoury/profile.php?user_id='.$id.'" target="_blank">'.$user_pic.'</a></div></td>
                  <td width="14%"><div  align="right">Name:</div></td>
                  <td width="63%"><a href = "http://localhost/newadamKhoury/profile.php?user_id='.$id.'" target="_blank">'.$firstname.' '.$lastname.'</a></td>
                  </tr>

                  <tr>
                    <td><div align="right">Birth date:</div></td>
                    <td>'.$birthdate.'</td>
                  </tr>
                  <tr>
                   <td><div align="right">Registered:</div></td>
                   <td>'.$registereddate.'</td>
                  </tr>
                  </table>
                  <hr />
          ';

 }//close while

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Search_Members</title>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Abel|Satisfy' rel='stylesheet' type='text/css' />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<!--[if IE 6]>
<link href="default_ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
</head>
<body>
<?php  require_once('header.php'); ?>
<div id="wrapper">
    <div id="page-wrapper">
        <div id="page">
            <div id="wide-content">


              <table width="94%" height="63">
                <tr>
                  <td width="29%"><form id="form1" method="post" action="member_search.php">

                  Browse Newest Members
                    <input type="submit" name="button" id="button" value="go" />
                    <input type="hidden" name="listbyq" value="newest_members" />
                  </form></td>
                  <td width="28%"><form id="form2" name="form2" method="post" action="member_search.php">
                    Search By specialization<br />
                    <select name="specialization"  class="formField">
                        <option value="0">-- Select Your Specialization --</option>

                          <?php specializationQuery(); ?>
                     </select>


                    <input type="submit" name="button2" id="button2" value="go" />
                    <input type="hidden" name="listbyq" value="by_specialization" />
                  </form></td>
                  <td width="43%"><form id="form3" name="form3" method="post" action="member_search.php">
                    Search By firstname
                    <label>
                    <input type="text" name = "fname" id="fname" />
                    </label>
                    <input type="submit" name="button3" id="button3" value="go" />
                    <input type="hidden" name="listbyq" value="by_firstname" />
                  </form></td>
                </tr>
              </table>
              <br />
              <table width="70%" align="center">
                <tr>
                  <td><?php print "$queryMSG";  ?>
                         <br />
                      <?php print "$outputlist";  ?>
                   </td>
                </tr>
              </table>
4

1 回答 1

0

您的代码似乎将排序字符串添加到查询语句的末尾。基本语句是:

SELECT user_id, first_name, last_name, birth_date, registered_date
FROM user

然后,您将添加以下字符串之一:

  $querySrting="WHERE registered_date!='' ORDER BY registered_date DESC "

  $querySrting="SELECT specialization_name FROM user u     INNER JOIN specialization s     ON u.specialization=s.specialization_id      WHERE u.user_id='$id'";

  $querySrting="WHERE  first_name LIKE '%$fname%'"or die(mysql_error());

嗯。其中一个不适合您的基本select . . . from陈述。要修复中间示例,您可以在order by语句中使用子查询:

  $querySrting="order by (SELECT specialization_name FROM user u     INNER JOIN specialization s     ON u.specialization=s.specialization_id      WHERE u.user_id='$id')";

或者,更简单的版本:

  $querySrting="order by (SELECT specialization_name FROM specialization s WHERE s.user_id=user.user_id";

然而,一个可读的特化可能是有用的,所以把它放在基本查询中:

SELECT user_id, first_name, last_name, birth_date, registered_date, specialization_name
FROM user u left outer join
     specialization s
     on u.specialization = s.specialization_id

只做中间排序:

  $querySrting="order by specialization_name";
于 2013-05-11T21:07:49.513 回答