php - Search MySQL database with LIKE statement -
i need return search results of users in database.
at moment, have managed use function in mysql query return results count, need echo usernames of results, not first result:
<?php // mysql details $host="localhost"; // host name $username="classified"; // mysql username $password="classified"; // mysql password $db_name="jaycraftcommunity"; // database name $tbl_name="members"; // table name // connect server , select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); // form variable local $search = $_post['username']; // make query //$sql = "select * $tbl_name (username='$search' or verified='$search')"; // sql query $sql = "select username $tbl_name username '%$search%'"; // sql query $result = mysql_query($sql); // query // handle query data $count = mysql_num_rows($result); $row = mysql_fetch_row($result); $array = mysql_fetch_array($result); echo $count; foreach( $row $arrays ): ?> <tr> <td><?php echo htmlspecialchars( $arrays ); ?></td> <br /> </tr> <?php endforeach; ?>
the html form looks this:
<h2>search</h2> <form method="post" action="search_engine.php"> search for: <input type="text" name="username"> <input type="submit" value="search username"> </form>
at moment, if go http://dev.jaycraft.co/player/dupe/search.php can see in action. search individual letters, show how many results there echo $count, echos first result.
thanks
every call of mysql_fetch_row($result)
return row , advance cursor. need use while() loop on rows in result:
while($row = mysql_fetch_row($result)) { echo $row[0]; }}
for ease of use, it's better use mysql_fetch_assoc($result);
return associative array:
while($row = mysql_fetch_assoc($result)) { echo $row['username']; }}
Comments
Post a Comment