PHP search by keyword from mysql
Dear experties,
I want to display data from Mysql by keyword but my code doesn't work.
Maybe you have suggestion for this environment.
please...
Related Article: PHP Search
is a solved PHP discussion thread by Shodow that has 1 reply, was last updated 7 months ago and has been tagged with the keywords: php, search, mysql.
HasNor
Junior Poster
101 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
would need to see the code to see what you are trying to accomplish.
ryantroop
Junior Poster
180 posts since Jun 2012
Reputation Points: 57
Solved Threads: 22
Skill Endorsements: 0
Thanks for reply.. there are my codes
?php
include 'connection/db_connect.php';
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="key"></label>
<input type="text" name="key" id="key">
<input type="submit" name="Submit" id="Submit" value="Submit">
</p>
<table width="200">
<tr>
<td>id</td>
<td>dept</td>
</tr>
<?php
if(isset($_POST["Submit"])){
$sql ="SELECT * FROM user WHEN name LIKE '{%key%}'";
$result=mysql_query($sql);
}
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['empNo']; ?></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php }?>
</table>
<p> </p>
</form>
HasNor
Junior Poster
101 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
the searching will act like DaniWeb search engine..
It will display result when the string entred contains in database field..
hurmm but my code doesn't work..
HasNor
Junior Poster
101 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
if(isset($_POST['key']{
$key = $_POST['key'];
$sql = "SELECT * FROM user WHEN name LIKE '{%$key%}'";
....
you probably dont even need the {} and you may want to escape $key before you pop it in your query.
ryantroop
Junior Poster
180 posts since Jun 2012
Reputation Points: 57
Solved Threads: 22
Skill Endorsements: 0
i had tried it but still no result..
it doesn't work..
HasNor
Junior Poster
101 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Sorry, not sure why that happened.. if you copy and pasted directly I missed two parentheses.
should be:
if(isset($_POST['key'])){
$key = $_POST['key'];
$sql = "SELECT * FROM user WHERE name like '%$key%';";
....
}
ryantroop
Junior Poster
180 posts since Jun 2012
Reputation Points: 57
Solved Threads: 22
Skill Endorsements: 0
My God!!
Thank you so much~~~ it's worked!!
yesterday i had tried change the code many ways but didn't put the ; qoute...
Awesome...Thank you s much ryantroop!
HasNor
Junior Poster
101 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
ryantroop
Junior Poster
180 posts since Jun 2012
Reputation Points: 57
Solved Threads: 22
Skill Endorsements: 0
Question Answered as of 9 Months Ago by
ryantroop