Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <?php
  2. $q = strtolower($_GET["q"]);
  3.  
  4. if (!$q) return;
  5.  
  6. $host = "localhost";
  7. $user = "root";
  8. $password = "";
  9. $database = "private_message_system";
  10.  
  11. //make connection
  12. $server = mysql_connect($host, $user, $password);
  13. $connection = mysql_select_db($database, $server);
  14.  
  15. $query = mysql_query("SELECT * FROM users");
  16.  
  17.  
  18. while($row = mysql_fetch_array($query)){
  19.  
  20.  
  21. $items = array($row["user_name"] => $row["user_email"]);
  22.  
  23. }
  24.  
  25. $result = array();
  26.  
  27. foreach ($items as $key=>$value) {
  28. if (strpos(strtolower($key), $q) !== false) {
  29.  
  30. array_push($result, array(
  31. "name" => $key,
  32. "to" => $value
  33. ));
  34. }
  35. }
  36.  
  37. echo json_encode($result);
  38. ?>
  39.  
  40. // here is where you get your to connection to the database
  41. $conn = mysql_connect("your IP", "username", "password");
  42. mysql_select_db("mydb", $conn);
  43.  
  44. // here you have to do the select to retrieve data from the table.
  45. $query = "SELECT `name`, `to` from mytable";
  46.  
  47. // now you got all the records but you still need to iterate over this result
  48. $result = mysql_query($query, $conn);
  49. $array = array();
  50.  
  51. // retrieve a record and append it to the array
  52. while($record = mysql_fetch_assoc($result)):
  53. $array[] = $record;
  54.  
  55. endwhile;
  56.  
  57. // please close the door....
  58. mysql_close($conn);
  59. echo json_encode($array);
  60.  
  61. <?php
  62.  
  63. // Get the query term from the url
  64. $q = strtolower($_GET["q"]);
  65.  
  66. // Do nothing if it's empty or not set
  67. if (empty($q)) return;
  68.  
  69. // Result array which we are going to get from MySQL
  70. $result= array();
  71.  
  72. // Make a SQL Connection
  73. mysql_connect("localhost", "admin", "password") or die(mysql_error());
  74.  
  75. // Try to connect to your DATABASE (change the name) or throw an error
  76. mysql_select_db("DATABASE") or die(mysql_error());
  77.  
  78. // Get data from the "email" table
  79. // Where the name field is LIKE the search term
  80. $result = mysql_query("SELECT * FROM email WHERE name LIKE '%".mysqli_real_escape_string($q)."%'")
  81. or die(mysql_error()); //throw an error if something went wrong
  82.  
  83. //Read all the results ($row) in a loop and put them in the result array
  84. while($row = mysql_fetch_array( $result )) {
  85. $result[] = array('name' => $row['name'], 'to' => $row['to']);
  86. }
  87.  
  88. // Output the array as JSON
  89. echo json_encode($result);
  90.  
  91. ?>
  92.  
  93. ini_set('display_errors', 1);
  94. error_reporting(E_ALL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement