Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $q = strtolower($_GET["q"]);
- if (!$q) return;
- $host = "localhost";
- $user = "root";
- $password = "";
- $database = "private_message_system";
- //make connection
- $server = mysql_connect($host, $user, $password);
- $connection = mysql_select_db($database, $server);
- $query = mysql_query("SELECT * FROM users");
- while($row = mysql_fetch_array($query)){
- $items = array($row["user_name"] => $row["user_email"]);
- }
- $result = array();
- foreach ($items as $key=>$value) {
- if (strpos(strtolower($key), $q) !== false) {
- array_push($result, array(
- "name" => $key,
- "to" => $value
- ));
- }
- }
- echo json_encode($result);
- ?>
- // here is where you get your to connection to the database
- $conn = mysql_connect("your IP", "username", "password");
- mysql_select_db("mydb", $conn);
- // here you have to do the select to retrieve data from the table.
- $query = "SELECT `name`, `to` from mytable";
- // now you got all the records but you still need to iterate over this result
- $result = mysql_query($query, $conn);
- $array = array();
- // retrieve a record and append it to the array
- while($record = mysql_fetch_assoc($result)):
- $array[] = $record;
- endwhile;
- // please close the door....
- mysql_close($conn);
- echo json_encode($array);
- <?php
- // Get the query term from the url
- $q = strtolower($_GET["q"]);
- // Do nothing if it's empty or not set
- if (empty($q)) return;
- // Result array which we are going to get from MySQL
- $result= array();
- // Make a SQL Connection
- mysql_connect("localhost", "admin", "password") or die(mysql_error());
- // Try to connect to your DATABASE (change the name) or throw an error
- mysql_select_db("DATABASE") or die(mysql_error());
- // Get data from the "email" table
- // Where the name field is LIKE the search term
- $result = mysql_query("SELECT * FROM email WHERE name LIKE '%".mysqli_real_escape_string($q)."%'")
- or die(mysql_error()); //throw an error if something went wrong
- //Read all the results ($row) in a loop and put them in the result array
- while($row = mysql_fetch_array( $result )) {
- $result[] = array('name' => $row['name'], 'to' => $row['to']);
- }
- // Output the array as JSON
- echo json_encode($result);
- ?>
- ini_set('display_errors', 1);
- error_reporting(E_ALL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement