当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP-自己给自己写接口

PHP-自己给自己写接口

2018年02月18日  | 移动技术网IT编程  | 我要评论

工具:
XAMPP 3.2.2(Apache+MySQL+PHP);
navicat for mysql

服务器:
阿里云/新浪云/百度应用/京东云

index.php
userName = $_userName;
        $this->age = $_age;
        $this->resume = $_resume;
        $this->userAccount = $_userAccount;
        $this->password = $_password;
    }

} 


$arr = array(
    'code'=>'400',
    'message'=>'Insert In Error'
);

//连接数据库
$conn = mysql_connect('127.0.0.1', 'root', '') or die('Error Info : '.mysql_error());
mysql_select_db('db_mxd', $conn);
$result_sql = mysql_query("select * from dong") or die('Error Info_1'.mysql_error());
$row = mysql_fetch_object($result_sql);

//拼接字符串
$result = array();

do{
    $obj = new Person($row->userName, $row->age, $row->resume, $row->userAccount, $row->password);
    array_push($result, $obj);
}while($row = mysql_fetch_object($result_sql));

if(count($result) > 0)
{
    $arr['code'] = '200';
    $arr['message'] = 'Success';
    $arr['result'] = $result;
}else{
    $arr['result'] = $result;
}

//关闭结果集
mysql_free_result($result_sql); 
//关闭MySQL服务器
mysql_close($conn); 
//echo
$tmp= json_encode($arr);
echo $callback.'('.json_encode($arr).')';
?>
//
        jQuery.ajax({
                url: "https://116.196.73.253/dong/index.php",
                type: "get",
                dataType: "jsonp",
                jsonp: "callback",
                jsonpCallback:"success_jsonpCallback",
                success: function (res) {
                    console.log(res)
                    $(".res").text("正确"+JSON.stringify(res))
                },

                error: function (e) {
                    console.log(e)
                    $(".res").text("错误"+JSON.stringify(e))
                }
            });

新建数据库 db_mxd
新建表 dong

DROP TABLE IF EXISTS `dong`;
CREATE TABLE `dong` (
  `id` varchar(255) DEFAULT NULL,
  `userName` varchar(255) DEFAULT NULL,
  `age` varchar(255) DEFAULT NULL,
  `resume` varchar(255) DEFAULT NULL,
  `userAccount` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of dong
-- ----------------------------
INSERT INTO `dong` VALUES ('1', '2', '3', '4', '5', '6');

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网