PHP中的LIKE语句

时间:2010-01-30 13:56:27

标签: php mysql

如果我想列出一个地址类似于用户输入的人的语法是什么?这是我的代码,请帮忙。表单输入将是地址。 我想做这样的事情:http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

 <?php
       $con = mysql_connect("localhost","root","");
       if (!$con)
        {
       die('Could not connect: ' . mysql_error());
      }

       mysql_select_db("Hospital", $con);

        $result = mysql_query("SELECT * FROM nais WHERE ADDRESS='{$_POST["address"]}'");

       echo "<table border='1'>
         <tr>
      <th>HospNum</th>
          <th>RoomNum</th>
           <th>LastName</th>
           <th>FirstName</th>
      <th>MidName</th>
            <th>Address</th>
         <th>TelNum</th>
          <th>Nurse</th>
       </tr>";

          while($row = mysql_fetch_array($result))
           {
                 echo "<tr>";
  echo "<td>" . $row['HOSPNUM'] . "</td>";
     echo "<td>" . $row['ROOMNUM'] . "</td>";
     echo "<td>" . $row['LASTNAME'] . "</td>";
  echo "<td>" . $row['FIRSTNAME'] . "</td>";
    echo "<td>" . $row['MIDNAME'] . "</td>";
      echo "<td>" . $row['ADDRESS'] . "</td>";
       echo "<td>" . $row['TELNUM'] . "</td>";
        echo "<td>" . $row['NURSE'] . "</td>";

     echo "</tr>";
   }
     echo "</table>";

      mysql_close($con);

1 个答案:

答案 0 :(得分:4)

试试这个:

$result = mysql_query("SELECT * FROM nais WHERE ADDRESS LIKE '%" . $_POST['address'] . "%';");

您还应该使用预备语句或mysql_real_escape_string(),请参阅SQL Injections