-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchQuery.php
59 lines (48 loc) · 1.16 KB
/
searchQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
# main
if(!empty($_POST['user-input']))
{
$data = array();
$errors = array();
echo json_encode(search($_POST['user-input']));
}
function search($query){
global $data;
global $errors;
require_once('db_setup.php');
$sql = "USE jjaco16;";
if ($conn->query($sql) !== TRUE) {
return databaseError($conn->error);
}
$table = get_post($conn, 'table');
$field = get_post($conn, 'field');
$query = "%{$query}%";
# set up query and post it to database
$stmt = $conn->prepare("SELECT * FROM $table WHERE $field LIKE ?");
if(!$stmt) return databaseError($conn->error);
$stmt->bind_param("s", $query);
$stmt->execute();
#store result
$result = $stmt->get_result();
$results = array();
$i = 0;
while($row = $result->fetch_assoc()){
$results[$i] = $row;
$i++;
}
$stmt->close();
$conn->close();
$data['results'] = $results;
return $data;
}
function databaseError($error){
global $data;
global $errors;
$errors['database'] = $error;
$data['errors'] = $errors;
return $data;
}
function get_post($database, $var){
return $database->real_escape_string($_POST[$var]);
}
?>