Horje Website
Search query by id Mysqli – PHP Code

Just modify line 9 according to your Mysql Username, Password and Database.

Then, use the search.

 

 
 
  1. <?php
  2. // php code to search data in mysql database and set it in input text
  3. if(isset($_POST['search']))
  4. {
  5. // id to search
  6. $id = $_POST['id'];
  7. // connect to mysql
  8. $connect = mysqli_connect("localhost", "root", "","test_db");
  9. // mysql search query
  10. $query = "SELECT `fname`, `lname`, `age` FROM `users` WHERE `id` = $id LIMIT 1";
  11. $result = mysqli_query($connect, $query);
  12. // if id exist
  13. // show data in inputs
  14. if(mysqli_num_rows($result) > 0)
  15. {
  16. while ($row = mysqli_fetch_array($result))
  17. {
  18. $fname = $row['fname'];
  19. $lname = $row['lname'];
  20. $age = $row['age'];
  21. }
  22. }
  23. // if the id not exist
  24. // show a message and clear inputs
  25. else {
  26. echo "Undifined ID";
  27. $fname = "";
  28. $lname = "";
  29. $age = "";
  30. }
  31. mysqli_free_result($result);
  32. mysqli_close($connect);
  33. }
  34. // in the first time inputs are empty
  35. else{
  36. $fname = "";
  37. $lname = "";
  38. $age = "";
  39. }
  40. ?>
  41. <!DOCTYPE html>
  42. <html>
  43. <head>
  44. <title> PHP FIND DATA </title>
  45. <meta charset="UTF-8">
  46. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  47. </head>
  48. <body>
  49. <form action="php_search_in_mysql_database.php" method="post">
  50. Id:<input type="text" name="id"><br><br>
  51. First Name:<input type="text" name="fname" value="<?php echo $fname;?>"><br>
  52. <br>
  53. Last Name:<input type="text" name="lname" value="<?php echo $lname;?>"><br><br>
  54. Age:<input type="text" name="age" value="<?php echo $age;?>"><br><br>
  55. <input type="submit" name="search" value="Find">
  56. </form>
  57. </body>
  58. </html>

 

Published:
December 20, 2022
Author:
admin
Category:
Mysql, PHP Source Code
Views:
30

This article was posted in Mysql, PHP Source Code. Bookmark the permalink. Follow comments with the RSS feed for this post.Post a Comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Horje © 2011 - 2023