LIKE Statement in PHP
What will be the syntax if I want to list a person with an address like the one inputted by the user?Here is my code, please help. The form input would be address. I want to do something like this: 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>Roo开发者_如何学编程mNum</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);
Try this:
$result = mysql_query("SELECT * FROM nais WHERE ADDRESS LIKE '%" . $_POST['address'] . "%';");
You should also use prepared statements or mysql_real_escape_string()
, see SQL Injections.
精彩评论