forked from T-Hugs/ValidCake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getComments.php
53 lines (45 loc) · 1.21 KB
/
getComments.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
<?php
//Connect to database
$user = 'root';
$pass = '';
$db = 'validcake';
$path = 'localhost';
$db_conn = mysql_connect($path, $user, $pass);
if (!mysql_select_db($db, $db_conn))
{
echo json_encode(array('status' => 'error', 'message' => 'Error connecting to database'));
exit;
}
//Build the query
$query = "SELECT * FROM `comments` ORDER BY `created` DESC LIMIT 25";
//Query the database
$result = mysql_query($query);
if (!$result)
{
echo json_encode(array('status' => 'error', 'message' => 'Error retrieving comments'));
exit;
}
$result_arr = res2arr($result);
//Return success and the data
echo json_encode(array('status' => 'success', 'data' => $result_arr));
exit;
/**
* Turns a mysql resource handle to an array
*/
function res2arr($handle)
{
$results = array();
while ($row = mysql_fetch_assoc($handle))
{
$row['created'] = date('D, M j, Y g:ia', strtotime($row['created']));
$row['gravatar_url'] = 'http://gravatar.com/avatar/'.md5(trim(strtolower($row['email']))).'?d=404&s=60';
unset($row['email']);
//Check if gravatar exists
$headers = get_headers($row['gravatar_url']);
if (strpos($headers[0], '404') !== false)
$row['gravatar_url'] = '';
$results[] = $row;
}
return $results;
}
?>