php连接Mysql数据库php连接Mysql数据库,查询数据转化为json

<?php
/*
 * Created on 2013-1-17
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
//连接数据库
$link = mysql_connect("127.0.0.1", "root", "root") or die("Could not connect:" . mysql_error());
//选择数据库
mysql_select_db("ecity", $link) or die("Can\'t use db:" . mysql_error());
//进行你想要的操作
mysql_query("set names 'utf8'");
?>

 

   查询语句:in , like,between and

if($area!=null){
		for($i=0;$i<count($arr);$i++){
			$s=$s.",'"."$arr[$i]"."'";
		}
		$s=trim($s,",");
//		echo $s;
		$sql = $sql . " and location in ($s) ";
	}
	if($type!=null){
		$sql = $sql . " and type like '%$type%' ";
	}
	if($price!=null){
		$sql = $sql . " and price between $arr_price[0] and $arr_price[1] ";
	}

 

查询数据转化为json:

function encode($str) {
	$code = json_encode($str);
	return preg_replace("#\\\u([0-9a-f]{4}+)#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $code);
}

$sql = "select * from tc_table ";
$query = mysql_query($sql) or die(mysql_error());
$rows = mysql_num_rows($query);
while ($row = mysql_fetch_object($query)) {
	$str[] = $row;
}

echo '{"Count":"'.$rows.'","list":'.encode($str).'}';

 

你可能感兴趣的:(PHP,数据库,查询数据,转化为json,连接Mysql)