PHP封裝返回Ajax字符串和JSON數(shù)組的方法
更新時間:2017年02月17日 09:05:35 投稿:jingxian
下面小編就為大家?guī)硪黄狿HP封裝返回Ajax字符串和JSON數(shù)組的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
<?php
class DBDA
{
public $host="localhost";
public $uid = "root";
public $pwd = "123";
public $dbname = "mydb";
//成員方法
public function Query($sql,$type=1)
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$r = $db->query($sql);
if($type==1)
{
return $r->fetch_all();
}
else
{
return $r;
}
}
//返回字符串的方法
public function StrQuery($sql,$type=1)
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$r = $db->query($sql);
if($type==1)
{
$attr = $r->fetch_all();
$str = "";
foreach($attr as $v)
{
$str .= implode("^",$v)."|";
}
return substr($str,0,strlen($str)-1);
}
else
{
return $r;
}
}
//返回JSON
function JSONQuery($sql,$type=1)
{
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$r = $db->query($sql);
if($type==1)
{
return json_encode($r->fetch_all(MYSQLI_ASSOC));
}
else
{
return $r;
}
}
}
以上這篇PHP封裝返回Ajax字符串和JSON數(shù)組的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP strcmp()和strcasecmp()的區(qū)別實例
這篇文章主要介紹了PHP strcmp()和strcasecmp()的區(qū)別實例的相關(guān)資料,需要的朋友可以參考下2016-11-11
tp5.1 框架數(shù)據(jù)庫高級查詢技巧實例總結(jié)
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫高級查詢技巧,結(jié)合實例形式總結(jié)分析了tp5.1 框架數(shù)據(jù)庫快捷查詢、區(qū)間查詢、批量查詢、閉包查詢等相關(guān)高級查詢使用技巧,需要的朋友可以參考下2020-05-05
基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用
本篇文章介紹了,基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用。需要的朋友參考下2013-04-04
laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
今天小編就為大家分享一篇laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

