php开发数据库接口 php开发接口的框架

php开发的对数据库的操作,用Java怎么实现

比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较PHP和Java的Web开发。以下是我就几个主要方面进行的比较:

呼中网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

一、 语言比较

PHP是解释执行的服务器脚本语言,首先php有简单容易上手的特点。语法和c语言比较象,所以学过c语言的程序员可以很快的熟悉php的开发。而java需要先学好java的语法和熟悉一些核心的类库,懂得面向对象的程序设计方法。所以java不如php好学。

Java首先要编译成字节码.class文件,然后在java虚拟机上解释执行。Java的Web开发首先最容易想到的就是JSP(现在已经到JSP2.0),原来的java的Web开发都是用servlet来实现的,用servlet来开发需要程序员在java的源文件中嵌入大量的html代码。所以后来就出现了JSP,JSP可以方便的嵌入到html文件当中,其实jsp文件在服务器上执行的时候首先会被应用服务器转换成servlet,然后再编译执行。Jsp可以通过servlet和JavaBean的支持产生强大的功能。JavaBean 是一种可复用的、跨平台的软件组件。使用javabean可以方便的实现java代码和html的分离,能够增强系统的功能和软件的复用性。

Java的Web开发属于SUN公司定义的J2EE其中的规范。而且在J2EE中包括了java的Web开发的所有方面,如:JSP、Servlet、JDBC、JNDI、JAVABEAN、EJB等等。J2EE就特别适合于做大型的企业级的应用。

二、 数据库访问比较

Java通过JDBC来访问数据库,通过不同的数据库厂商提供的数据库驱动方便地访问数据库。访问数据库的接口比较统一。

PHP对于不同的数据库采用不同的数据库访问接口,所以数据库访问代码的通用性不强。例如:用Java开发的Web应用从MySQL数据库转到Oracle数据库只需要做很少的修改。而PHP则需要做大量的修改工作。

三、 系统设计架构比较

采用Java的Web开发技术,需要使用的是面向对象的系统设计方法,而PHP还是采用面向过程的开发方法。所以用Java进行开发前期需要做大量的系统分析和设计的工作。

四、 跨平台性

Java和PHP都有很好的跨平台的特性。几乎都可以在不作任何修改的情况下运行在Linux或者Windows等不同的操作系统上。

五、 开发成本比较

PHP最经典的组合就是:PHP + MySQL + Apache。非常适合开发中小型的Web应用,开发的速度比较快。而且所有的软件都是开源免费的,可以减少投入。

Java的Web应用服务器有免费Tomcat、JBoss等,如果需要更好的商业化的服务有:Web Sphere和 Web logic。

六、 分布式多层架构比较

PHP只能实现简单的分布式两层或三层的架构,而JAVA在这方面就比较强大,可以实现多层的网络架构。数据库层(持久化层)、应用(业务)逻辑层、表示逻辑层彼此分开,而且现在不同的层都已经有一些成熟的开发框架的支持。例如Struts就是利用java的Web开发技术实现了MVC的设计模式,而在业务逻辑层也有Spring框架,数据库持久化层有Hibernate等框架。这些框架可以方便开发者高效、合理、科学得架构多层的商业应用。

下面简要的说一下Struts,它实质上是在JSP Model2的基础上实现的一个MVC(Model、View、Controler)框架。JSP Model2体系结构是一种联合使用JSP 与Servlet 来提供动态内容的方法。在Struts框架中,模型由实现业务逻辑的JavaBean或EJB组件构成,控制器由Servlet实现的,视图由一组JSP文件组成。采用Struts可以明确角色的定义和开发者与网页设计者的分工。而且项目越复杂,其优势越明显。

七、 源代码安全

PHP开发的程序的源代码都是公开的,他人拿到php开发的程序后都可以进行修改。

Java开发的程序,最后用户拿到的是只是一些编译好的class类,无法看到完整的源代码,安全性高。

八、性能比较

有人做过试验,对这两种种语言分别做回圈性能测试及存取Oracle数据库测试。

在循环性能测试中,JSP只用了令人吃惊的四秒钟就结束了20000*20000的回圈。而PHP测试的是2000*2000循环(少一个数量级),却分别用了63秒。

数据库测试中,二者分别对 Oracle 8 进行 1000 次 Insert,Update,Select和Delete: JSP 需要 13 秒,PHP 需要 69 秒。

综上所述,我个人认为,PHP适合于快速开发,中小型应用系统,开发成本低,能够对变动的需求作出快速的反应。而Java适合于开发大型的应用系统,应用的前景比较广阔,系统易维护、可复用性较好。还有,同样功能的系统用Java开发的系统要比PHP开发的系统的价格要高。

Php开发手机端接口,假设从数据库拿好数据,是不是直接return 一个json encode字符

用json 格式返回是正确做法,但你说的访问这个方法不确切,应该是向php提交get或post请求

php中怎么把数据库连接写成一个接口

我自己封装的一个

?php

class AppConfig{

public static $dbParam = array(

'dbHost' = 'localhost',

'dbUser' = 'root',

'dbPassword' ='',

'dbName' = '数据库名',

'dbCharset' = 'utf8',

'dbPort' = 3306,

'dbPrefix' = 'test_',

'dbPconnect' = 0,

'dbDebug' = true,

);

}

class Model {

private $version = ''; //mysql版本

private $config = array(); //数据库配置数组

private $class; //当前类名

public $tablepre = 'ts_'; //表前缀

public $db = ''; //库名

public $table = ''; //表名

private static $link; //数据库链接句柄

private $data = array(); //中间数据容器

private $condition = ''; //查询条件

private $fields = array(); //字段信息

private $sql = array(); //sql集合,调试用

public $primaryKey = 'id'; //表主键

//构造函数初始化

public function __construct($dbParam = array()) {

$this-config = (is_array($dbParam) !empty($dbParam)) ? $dbParam : AppConfig::$dbParam;

$this-connect();

$this-init();

}

//链接数据库

private function connect() {

if($this-config['dbPconnect']) {

self::$link = @mysql_pconnect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword']);

}else{

self::$link = @mysql_connect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword'], true);

}

mysql_errno(self::$link) != 0 $this-errdie('Could not connect Mysql: ');

$this-db= !empty($this-db) ? $this-db : $this-config['dbName'];

$serverinfo = $this-version();

if ($serverinfo '4.1' $this-config['dbCharset']) {

mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);

}

if ($serverinfo '5.0') {

mysql_query("SET sql_mode=''", self::$link);

}

@mysql_select_db($this-db, self::$link) or $this-errdie('Cannot use database');

return self::$link;

}

//表基本信息初始化

protected function init() {

$this-class = get_class($this);

$this-table = !empty($this-table) ? $this-table : strtolower($this-class);

$this-table = $this-tablepre . $this-table;

return $this;

}

//设置属性值

public function __set($name, $value) {

//exit($value);

$this-data['fields'][$name] = $value;

}

//获取属性值

public function __get($name) {

if(isset($this-data['fields'][$name])) {

return($this-data['fields'][$name]);

}else {

return NULL;

}

}

//字段信息处理

private function implodefields($data) {

if (!is_array($data)) {

$data = array();

}

$this-fields = !empty($this-data['fields']) ? array_merge($this-data['fields'], $data) : $data;

foreach($this-fields as $key = $value) {

$fieldsNameValueStr[] = "`$key`='$value'";

$fieldsNameStr[] = "`$key`";

$fieldsValueStr[] = "'$value'";

}

return array($fieldsNameValueStr, $fieldsNameStr, $fieldsValueStr);

}

//条件判断组装

private function condition($where = NULL) {

if (is_numeric($where)) {

$where = "WHERE `{$this-primaryKey}`='{$where}' LIMIT 1";

}elseif (is_array($where)){

$where = "WHERE `{$this-primaryKey}` in (".implode(',',$where).")";

}elseif(!empty($this-data['condition'])){

//'预留WHERE', 'order', 'group', 'limit' …………等条件关键词处理接口

$where = $where ? "WHERE {$where}" : "WHERE 1";

isset($this-data['condition']['where']) $where .= ' AND '.$this-data['condition']['where'];

isset($this-data['condition']['group']) $where .= ' GROUP BY '.$this-data['condition']['group'];

isset($this-data['condition']['order']) $where .= ' ORDER BY '.$this-data['condition']['order'];

isset($this-data['condition']['limit']) $where .= ' LIMIT '.$this-data['condition']['limit'];

}else{

$where = "WHERE {$where}";

}

$this-condition = $where;

return $this;

}

//插入数据

public function insert($data = array(), $replace = false) {

$fields = $this-implodefields($data);

$insert = $replace ? 'REPLACE' : 'INSERT';

$sql = "{$insert} INTO `{$this-db}`.`{$this-table}` (".implode(', ',$fields[1]).") values (".implode(', ',$fields[2]).")";

$this-query($sql);

return $this-getInsertId();

}

//更新数据

public function update($data = array() ,$where = '') {

$numargs = func_num_args();

if ($numargs == 1) {

$where = $data;

$data = array();

}

$fields = $this-implodefields($data);

$this-condition($where);

$sql = "UPDATE `{$this-db}`.`{$this-table}` SET ".implode(', ',$fields[0])." {$this-condition}";

$this-query($sql);

return $this-getAffectedRows();

}

//删除数据

public function delete($where = NULL) {

if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'delete'){

$sql = $where;

}else{

$this-condition($where);

$sql = "DELETE FROM `{$this-db}`.`{$this-table}` {$this-condition}";

}

$this-query($sql);

return $this-getAffectedRows();

}

//查询数据

public function select($where = NULL, $fields = '*') {

if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'select'){

$sql = $where;

}else{

$this-condition($where);

$sql = "SELECT {$fields} FROM `{$this-db}`.`{$this-table}` {$this-condition}";

}

return $this-fetch($this-query($sql));

}

//查询一条数据

public function getOne($where, $fields = '*') {

$data = $this-select($where, $fields = '*');

if($data) {

return $data[0];

}

return array();

}

//查询多条数据

public function getAll($where, $fields = '*') {

$data = $this-select($where, $fields = '*');

return $data;

}

//结果数量

public function getCount($where = '', $fields = '*') {

$this-condition($where);

$sql = "SELECT count({$fields}) as count FROM `{$this-db}`.`{$this-table}` {$this-condition}";

$data = $this-query($sql);

if($data){

return @mysql_result($data,0);

}

return 0;

}

//执行sql语句(flag为0返回mysql_query查询后的结果,为1返回lastid,其他返回影响行数,默认为2返回影响行数)

public function query($sql, $flag = '0', $type = '') {

if ($this-config['dbDebug']) {

$startime = $this-microtime_float();

}

//查询

if ($type == 'UNBUFFERED' function_exists('mysql_unbuffered_query')) {

$result = @mysql_unbuffered_query($sql, self::$link);

} else {

//exit($sql);

$result = @mysql_query($sql, self::$link);

}

//重试

if (in_array(mysql_errno(self::$link), array(2006,2013)) empty($result) $this-config['dbPconnect']==0 !defined('RETRY')) {

define('RETRY',true); @mysql_close(self::$link); sleep(2);

$this-connect();

$result = $this-query($sql);

}

if ($result === false) {

$this-errdie($sql);

}

if ($this-config['dbDebug']) {

$endtime = $this-microtime_float();

$this-sql[] = array($sql,$endtime-$startime);

}

//清空操作数据

$this-data = array();

return $flag == '0' ? $result : ($flag == '1' ? $this-getInsertId() : $this-getAffectedRows());

}

//返回结果$onlyone为true返回一条否则返回所有,$type有MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH

public function fetch($result, $onlyone = false, $type = MYSQL_ASSOC) {

if($result){

if ($onlyone) {

$row = @mysql_fetch_array($result, $type);

return $row;

}else{

$rowsRs = array();

while($row=@mysql_fetch_array($result, $type)) {

$rowsRs[] = $row;

}

return $rowsRs;

}

}

return array();

}

//可以运行SELECT,SHOW,EXPLAIN 或 DESCRIBE 等返回一个资源标识符的语句得到返回结果数组

public function show($sql, $onlyone = false) {

return $this-fetch($this-query($sql), $onlyone);

}

// 使用call函数处理同类型函数

private function __call($name, $arguments) {

$callArr = array('on', 'where', 'order', 'between', 'group', 'limit');

if (in_array($name, $callArr)) {

$this-data['condition'][$name] = $arguments[0];

}else{

$this-errdie("function error: function {$name} is not in ($this-class) class exist");

}

return $this;

}

//返回最后一次插入ID

public function getInsertId() {

return @mysql_insert_id(self::$link);

}

//返回受影响行数

public function getAffectedRows() {

return @mysql_affected_rows(self::$link);

}

//获取错误信息

private function error() {

return ((self::$link) ? @mysql_error(self::$link) : @mysql_error());

}

//获取错误信息ID

private function errno() {

return ((self::$link) ? @mysql_errno(self::$link) : @mysql_errno());

}

//获取版本信息

function version() {

if(empty($this-version)) {

$this-version = mysql_get_server_info(self::$link);

}

return $this-version;

}

//打印错误信息

private function errdie($sql = '') {

if ($this-config['dbDebug']) {

die('/BRBMySQL ERROR/B/BR

SQL:'.$sql.'/BR

ERRNO:'.$this-errno().'/BR

ERROR:'.$this-error().'/BR');

}

die('DB ERROR!!!');

}

//获取时间微妙数

private function microtime_float()

{

list($usec, $sec) = explode(" ", microtime());

return ((float)$usec + (float)$sec);

}

//析构函数

public function __destruct() {

echo 'hr';

$this-config['dbDebug'] print_r($this-sql);

//unset($this-result);

//unset($this-condition);

//unset($this-data);

}

}

class user extends Model {

//public $db = 'qsf_mvc';

//public $table = 'user';

public $primaryKey = 'uid';

}

$userObj = new user();

//---------------------------------------插入数据方法一-----------------------------------------

//模拟ActiveRecord模式 插入数据

$userObj-username = 'hoho';

$userObj-passwd = '1478522';

$userObj-email = 'qsf.z11@163.com';

$userObj-sex = 1;

$userObj-desc = '清洁工';

$insetId = $userObj-insert();

if ($insetId 0) {

echo "插入ID为:{$insetId}BR";

}

//---------------------------------------插入数据方法二-----------------------------------------

//直接数组做参数插入数据

$userArr = array(

'username' = 'hoho',

'passwd' = '1478522',

'email' = 'qsf.z2121ia@163.com',

'sex' = '1',

'desc' = '厨师',

);

$insetId = $userObj-insert($userArr);

if ($insetId 0) {

echo "插入ID为:{$insetId}BR";

}

//---------------------------------------更新数据方法一----------------------------------------

$userObj-username = 'h111oho';

$userObj-passwd = '1478511122';

$userObj-email = 'qsf111ia@163.com';

$userObj-sex = 1;

$userObj-desc = '清洁工';

$affectedRows1 = $userObj-update(89);

if ($affectedRows1 0) {

echo "影响行数为:{$affectedRows1}BR";

}

//---------------------------------------更新数据方法二----------------------------------------

//更新记录(传递参数的方式和insert操作一样)

$userArr = array(

'username' = 'hohoho',

'passwd' = '1474rr4448522',

'email' = 'qsf.rrza@165.com',

'sex' = '0',

'desc' = '厨师qq',

);

$affectedRows = $userObj-update($userArr, $insetId);

if ($affectedRows 0) {

echo "影响行数为:{$affectedRows}BR";

}

//----------------------------------------查询数据----------------------------------------------

$userRs0 = $userObj-select(8); //单个主键值

//print_r($userRs0);

$userRs1 = $userObj-select(array(1,5,8)); //多个主键值的数组

//print_r($userRs1);

$userRs2 = $userObj-select('select count(*) as count from user where uid 20'); //直接完整sql语句

//print_r($userRs2);

$userRs3 = $userObj-select("`uid` 0"); //where条件

//print_r($userRs3);

$userRs4 = $userObj-getOne("`uid` 0"); //获取单条记录

//print_r($userRs4);

$usersRs5 = $userObj-getAll("`uid` 0"); ////获取所有记录

//print_r($usersRs5);

$usersRs6 = $userObj-limit('0,10')-where('uid 100')-order('uid DESC')-group('username')-select();

//print_r($usersRs6);

//----------------------------------------删除数据-----------------------------------------------

//删除操作传递参数的方式和select操作一样

$userObj-delete(60); //单个主键值

$userObj-delete(array(1,5,8)); //多个主键值的数组

$userObj-delete('delete from user where uid 100'); //直接完整sql语句

$userObj-delete("`uid` 100"); //where条件

$userObj-limit('5')-where('uid 80')-delete();

//----------------------------------------特殊查询-----------------------------------------------

$userShowRs = $userObj-show('show create table user', true); //获取特殊查询的结果,第二个参数代表返回一条结果还是所有的结果

分享题目:php开发数据库接口 php开发接口的框架
网站路径:https://www.cdcxhl.com/article18/doddpgp.html

成都网站建设公司_创新互联,为您提供网站排名自适应网站定制网站建站公司电子商务企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化