欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP-自己给自己写接口

程序员文章站 2022-10-17 13:53:12
工具: XAMPP 3.2.2(Apache+MySQL+PHP); navicat for mysql 服务器: 阿里云/新浪云/百度应用/京东云 index.ph...

工具:
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).')';
?>
//index.html
        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');