Not receiving Resource ID from PHP MySQL Query
I tried echoing the value $query from a mysql_query request, but I am not receiving the resource #id that I usually receive. Please could you tell me what the problem is with the following code. Also, I would appreciate it if you could tell me how I could firstly get a for loop going to get the data from the query and secondly json encode it.
I would like 开发者_运维百科the data from the query to be parsed as json like this:
Array of dictionaries, dictionary looking like this:
click_id = ....
shorten_url = ....
referrer = ....
ip_address = ....
country_code = ....
PHP:
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
mysql_select_db("nucleusi_mkdev") or die("Unable to select db");
$query = "SELECT `click_id` , `shorturl` , `referrer` , `ip_address` , `country_code`FROM yourls_log";
$result = mysql_query($query);
echo $result;
?>
The resource id could change per request. So I wouldn't focus on that. If you're curious about the results you can do mysql_num_rows($result);
The docs have great code samples. Check out mysql_fetch_assoc() for looping over your query results and json_encode() for encoding each record.
A quick snippet to demo the two together:
$records = array();
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
array_push($records, $row);
}
echo json_encode($records);
精彩评论