We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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...

2
Contributors
8
Replies
23 Hours
Discussion Span
9 Months Ago
Last Updated
9
Views
Question
Answered
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>&nbsp;</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

Youre welcome :)

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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1023 seconds using 2.7MB