标签:
<?php header("Content-type: text/html; charset=utf-8"); header(‘Access-Control-Allow-Origin:*‘); $BiaDuak = ‘************‘;//定义变量 使用时 echo $BiaDuak; define("accessKeyId","************");//定义常量 使用时 echo accessKeyId; define("accessKeySecret","************"); define("endpoint","************"); //公共类 class DbHelp { private $servername="************.com:4065"; private $username="root" ; private $password="w386161506"; private $dbname="allhelp_db"; public $conn; private static $_instance; public static function getInstance() { if(! (self::$_instance instanceof self) ) { self::$_instance = new self(); } return self::$_instance; } private function __construct(){ $this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname); } private function __clone(){} } //公共函数 function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8) .substr($charid, 8, 4) .substr($charid,12, 4) .substr($charid,16, 4) ; return $uuid; } function guid(){ if (function_exists(‘com_create_guid‘)){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; } } function createguid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) ; return $uuid; } //用于继承的基类 class DbHelpBase { public $servername; public $username; public $password; public $dbname; public $conn; public function DbHelpBase(){ $this->servername="************:4065"; $this->username="root" ; $this->password="w386161506"; $this->dbname="allhelp_db"; $this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname); } public function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } public function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8) .substr($charid, 8, 4) .substr($charid,12, 4) .substr($charid,16, 4) ; return $uuid; } } //用于组合的类 class DbHelpField { public $servername; public $username; public $password; public $dbname; public $conn; private static $_instance; public static function getInstance() { if(! (self::$_instance instanceof self) ) { self::$_instance = new self(); } return self::$_instance; } private function __construct(){ $this->servername="************:4065"; $this->username="root" ; $this->password="w386161506"; $this->dbname="allhelp_db"; $this->conn=new mysqli($this->servername,$this->username,$this->password,$this->dbname); } private function __clone(){} public function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } public function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8) .substr($charid, 8, 4) .substr($charid,12, 4) .substr($charid,16, 4) ; return $uuid; } } ?>
<?php header("Content-type: text/html; charset=utf-8"); header(‘Content-Type: application/json‘); header(‘Access-Control-Allow-Origin:*‘); require "config.php"; class DataUser { private $conn; public function DataUser() { $dbcontext=DbHelp::getInstance(); $this->conn=$dbcontext->conn; if($this->conn->connect_error) { die("Connection failed: " . $conn->connect_error); } } public function Query_alluser() { $sql = "SELECT * FROM users"; $result = $this->conn->query($sql); $data =array(); if ($result->num_rows > 0) { while($rowObject = $result->fetch_object()) { $data[]=$rowObject; } $json = json_encode($data);//把数据转换为JSON数据. echo "{".‘"Comment"‘.":".$json."}"; } else { echo "0 results"; } } } echo "直接使用config.php中的类"; $t=new DataUser(); $t->Query_alluser(); echo "\n"; class DataUser1 extends DbHelpBase { public function Query_alluser() { $sql = "SELECT * FROM users"; $result = $this->conn->query($sql); $data =array(); if ($result->num_rows > 0) { while($rowObject = $result->fetch_object()) { $data[]=$rowObject; } $json = json_encode($data);//把数据转换为JSON数据. echo "{".‘"Comment"‘.":".$json."}"; } else { echo "0 results"; } } } echo "继承config.php中的类"; $tt=new DataUser1(); echo $tt->servername; echo $tt->create_guid(); echo $tt->Query_alluser(); echo "\n"; class DataUser2 { public $conn; public $formDbHelp; public function DataUser2() { $formDbHelp=DbHelpField::getInstance(); $this->conn= $formDbHelp->conn; } public function Query_alluser() { $sql = "SELECT * FROM users"; //$result = $this->formDbHelp->conn->query($sql);//PHP中好像不能三级访问古多定义一个$conn字段 $result = $this->conn->query($sql); $data =array(); if ($result->num_rows > 0) { while($rowObject = $result->fetch_object()) { $data[]=$rowObject; } $json = json_encode($data);//把数据转换为JSON数据. echo "{".‘"Comment"‘.":".$json."}"; } else { echo "0 results"; } } public function CreateGuid() { echo $this->formDbHelp->create_guid();//错误 } } echo "组合config.php中的类"; $ttt=new DataUser2(); echo $ttt->Query_alluser(); //echo $ttt->formDbHelp->create_guid();//错误 ?>
标签:
原文地址:http://www.cnblogs.com/wangboke/p/5594794.html