- 下载wamp:web集成开发环境
w:windows a:apache m:mysql p:php - 下载一个Navicat Premium,mysql可视化图形工具
- 下载一个php编辑器,我暂时用的sublime
如何通过浏览器访问php运行代码的结果?
将php代码项目复制到wamp安装目录下的名字叫www的文件夹,并且修改index.php文件名,因为默认访问路径为index.php文件,然后在浏览器上输入localhost,然后就行选择项目文件夹,选择要运行的php文件。
通信数据的标准格式:
- code : 状态码
- message : 提示信息,返回成功或者失败的提示
- code : 返回的数据
下面是demo代码:将下面几个文件放在项目目录下可以运行
<?php
require_once('./response.php'); //引入当前目录下的php文件
require_once('./db.php');
$page=isset($_GET['page'])?$_GET['page']:1; //$_GET为get请求获取的参数,post请求则为$_POST[]
$connect=Db::getInstance()->connect(); //Db::getInstance()->connect(); 为一个链接数据库的工具类,具体代码在下面
$sql='select * from test';
$result=mysql_query($sql,$connect); //执行sql语句的结果
$datas=array();
while($data=mysql_fetch_assoc($result)){ //获取查询的每一数据
$datas[]=$data; //为数组里的每一个字段赋值
}
if(is_numeric($page)){ //判断page是否为数字
Response::show("200",'数据获取成功!',$datas); //Response::show("200",'数据获取成功!',$datas);//将数据转化为json的一个工具里欸
}else {
Response::show("401",'数据获取失败!');
};
?>
下面是将数据转化为json格式的工具类
<?php
class Response {
const JSON = "json";
/**
* 按综合方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* @param string $type 数据类型
* return string
*/
public static function show($code, $message = '', $data = array(), $type = self::JSON) {
if(!is_numeric($code)) {
return '';
}
$type = isset($_GET['format']) ? $_GET['format'] : self::JSON; //判断浏览器请求返回的数据格式是json还是xml格式
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
if($type == 'json') {
self::json($code, $message, $data);
exit;
} elseif($type == 'array') {
var_dump($result);
} elseif($type == 'xml') {
self::xmlEncode($code, $message, $data);
exit;
} else {
// TODO
}
}
/**
* 按json方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* return string
*/
public static function json($code, $message = '', $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data
);
echo self::myJSON($result);
exit;
}
/**
* 按xml方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* return string
*/
public static function xmlEncode($code, $message, $data = array()) {
if(!is_numeric($code)) {
return '';
}
$result = array(
'code' => $code,
'message' => $message,
'data' => $data,
);
header("Content-Type:text/xml");
$xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml .= "<root>\n";
$xml .= self::xmlToEncode($result);
$xml .= "</root>";
echo $xml;
}
public static function xmlToEncode($data) {
$xml = $attr = "";
foreach($data as $key => $value) {
if(is_numeric($key)) {
$attr = " id='{$key}'";
$key = "item";
}
$xml .= "<{$key}{$attr}>";
$xml .= is_array($value) ? self::xmlToEncode($value) : $value;
$xml .= "</{$key}>\n";
}
return $xml;
}
/**************************************************************
*
* 将数组转换为JSON字符串(兼容中文)
* @param array $array 要转换的数组
* @return string 转换得到的json字符串
* @access public
*
*************************************************************/
public static function myJSON($array) {
self::arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
/**************************************************************
*
* 使用特定function对数组中所有元素做处理
* @param string &$array 要处理的字符串
* @param string $function 要执行的函数
* @return boolean $apply_to_keys_also 是否也应用到key上
* @access public
*
*************************************************************/
public static 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)) {
self::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--;
}
}
下面是连接数据库的工具类
<?php
class Db {
static private $_instance;
static private $_connectSource;
private $_dbConfig = array(
'host' => '127.0.0.1', //指定本机的ip地址
'user' => 'root', //mysql的账号
'password' => '', //mysql的密码
'database' => 'demo', //数据库名称
);
private function __construct() {
}
/**
* 单例模式
*/
static public function getInstance() {
if(!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function connect() {
if(!self::$_connectSource) {
self::$_connectSource = @mysql_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']);
if(!self::$_connectSource) {
throw new Exception('mysql connect error ' . mysql_error());
//die('mysql connect error' . mysql_error());
}
mysql_select_db($this->_dbConfig['database'], self::$_connectSource);
mysql_query("set names UTF8", self::$_connectSource);
}
return self::$_connectSource;
}
}