PHP代码
- <?php
- header("Content-Type: text/html; charset=utf-8");
- $mydb=mysql_connect("localhost","root","root");
- if (!$mydb){
- die('Could not connect:'. mysql_error());
- }
- $db_selected=mysql_select_db("mysql",$mydb);
- //$sql = "SELECT * from Person WHERE Lastname='Adams'";
- $sql="SELECT * from user";
- $result=mysql_query($sql,$mydb);
- //print_r(mysql_fetch_array($result));
- //处理输出数组格式
- //$db1=mysql_query("select * from `tb_info`");
- /*
- $arr=array();
- while($rows=mysql_fetch_array($db1)){
- $key=$rows['id']
- $arr[$key] = $rows['qucount']
- }
- */
- /**************************************************************
- *
- * 使用特定function对数组中所有元素做处理
- * @param string &$array 要处理的字符串
- * @param string $function 要执行的函数
- * @return boolean $apply_to_keys_also 是否也应用到key上
- * @access public
- *
- *************************************************************/
- function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
- {
- static $recursive_counter = 0;
- if (++$recursive_counter > 1000) {
- die('possible deep recursion attack');
- }
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- arrayRecursive($array[$key], $function, $apply_to_keys_also);
- } else {
- $array[$key] = $function($value);
- }
- if ($apply_to_keys_also && is_string($key)) {
- $new_key = $function($key);
- if ($new_key != $key) {
- $array[$new_key] = $array[$key];
- unset($array[$key]);
- }
- }
- }
- $recursive_counter--;
- }
- /**************************************************************
- *
- * 将数组转换为JSON字符串(兼容中文)
- * @param array $array 要转换的数组
- * @return string 转换得到的json字符串
- * @access public
- *
- *************************************************************/
- function JSON($array) {
- arrayRecursive($array, 'urlencode', true);
- $json = json_encode($array);
- return urldecode($json);
- }
- $array = array
- (
- 'Name'=>'希亚',
- 'Age'=>20
- );
- /*
- $array=array (
- 0 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 1 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 2 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '25',
- 'name' => '男士',
- 'province' => '上海',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 3 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '186****1046',
- ),
- 4 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '200元的爱玛电动车代金券',
- 'mobile' => '186****1046',
- ),
- 5 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 6 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 7 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 8 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '河南',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 9 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 10 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '1',
- 'photoPath' => '/201412/11/11/49/1418269782350A03EA57_c.jpg',
- ),
- 'age' => '20',
- 'name' => '白日做梦',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- );
- */
- echo JSON($array);
- ?>