dbphp
‘壹’ php语句中的 $db->query($sql); 请问这个->符号是什么 以及他的作用。
$db 是一个实例化好的数据库类,query是这个类里面的一个方法,$db->query($sql); 就是访问$db类里的query方法,->相当于访问类里面方法的一个语法而已。操作方法如下:
1、首先写上这个程序的注释内容,#创建一个类并实例化它。
‘贰’ php 如何输出DB类上所有SQL语句,页面上的所有 有关mysql 连接,语句 等信息
class DB_MYSQL
{
/*
* @_conf Array: [hostname],[username],[password],[dataname],[dbtype],[charset],[debug]
*/
var $_conf;
var $_dblink; //mysql connect
var $_dberr =0;
var $_sqlnum=0; //SQL query times
var $_sql='';
var $pageinfo =array();
var $pageSize =20; //=0: all; >0 page size
var $pageOrder=0;
var $stripSet =0;
function DB_MYSQL($db_config=''){
if(empty($db_config)){
global $db_conf;
$db_config=$db_conf[0];
}
$this->_conf=$db_config;
}
function conn()
{
$this->_dblink=@mysql_connect($this->_conf['hostname'],$this->_conf['username'], $this->_conf['password'])
OR $this->debug("mysql_connect error:".str_replace($this->_conf['username'].'@','',mysql_error($this->_dblink)));
if($this->_dblink)
{
if (isset($this->_conf['charset']) and strlen($this->_conf['charset'])>0)
{
@mysql_query("SET character_set_connection=".$this->_conf['charset'].", character_set_results=".$this->_conf['charset'].", character_set_client=binary", $this->_dblink);
}
$res=@mysql_select_db($this->_conf['dataname'],$this->_dblink)
OR $this->debug("mysql_select_db error:".mysql_error($this->_dblink));
if ($res) return true;
}
$this->_dberr=1;
return false;
}
function select_db($NewDB){
$this->_conf['dataname']=$NewDB;
if(empty($this->_dblink)){
return $this->conn();
}
$res=@mysql_select_db($this->_conf['dataname'])
OR $this->debug("mysql_select_db error:".mysql_error($this->_dblink));
if ($res) return true;
$this->_dberr=1;
return false;
}
/*
* @array query ($fetchType>0)
* @result query ($fetchType=0)
*/
function query($str,$fetchType=0,$file_rows='')
{
if (strlen($str)<1){
return false;
}
if(!$this->_dblink){
$this->conn();
}
else if (!mysql_ping($this->_dblink))
{
$this->close();
$this->conn();
}
$SQL=substr($str,0,18).'..';
if ($this->_conf['debug'] > 2)
{
$SQL=$str;
}
if(!empty($file_rows))
{
$SQL=$SQL.'<BR>'.$file_rows;
}
$this->_sql=$str;
$result=@mysql_query($str,$this->_dblink) OR $this->debug("mysql_query error:".mysql_error(),$SQL);
$this->_sqlnum=$this->_sqlnum+1;
if ($result)
{
if ($fetchType==1)
{
$arr=@mysql_fetch_array($result,1);
$this->check_query_quote($arr);
$this->free($result);
return $arr;
}
else if ($fetchType==2)
{
$arr = array();
while ($row = mysql_fetch_array($result,1))
{
$this->check_query_quote($row);
$arr[] = $row;
}
$this->free($result);
return $arr;
}
return $result;
}
else if ($fetchType>1) # NEW 2010-7-28
{
return array();
}
return false;
}//END sql
/*
* @array query_page
*/
function query_page(&$page,$Field,$Table,$Where='',$Order='',$Group='')
{
$page=intval($page);
$psize=abs($this->pageSize);
$this->_table=$Table;
if($psize<0 or $psize>300) $psize=20;
$sql="SELECT count(*) as allnum FROM $Table $Where";
if (strlen($Group)>0) {
$this->_sql="$sql $Group";
$result=$this->query("$sql $Group");
$rowsnum=$this->num_rows($result);
}
else{
$this->_sql=$sql;
$row=$this->query($sql,1);
$rowsnum=$row['allnum']+0;
}
if($rowsnum==0){
$page=0;
$this->pageinfo=Array('page'=>0,'pagenum'=>0,'rowsnum'=>0);
return array();
}
$pagenum=$psize<1 ? 1 : ceil($rowsnum/$psize);
$limit='';
if($psize==0){
$page=1;
$pagenum=1;
}
else
{
if($page > $pagenum or $page < 1) $page=1;
if($pagenum > 1){
$limit='limit '.(($page-1)*$psize).','.$psize;
}
}
$this->pageinfo['page'] =$page;
$this->pageinfo['pagenum'] =$pagenum;
$this->pageinfo['rowsnum'] =$rowsnum;
$sql ="SELECT $Field FROM $Table $Where $Group $Order $limit";
$this->_sql=$sql;
return $this->query($sql,2);
}
/*
* @array fetch_array
* @type: 1 MYSQL_ASSOC 2 MYSQL_NUM #3 MYSQL_BOTH
*/
function fetch_array(&$result,$type=1)
{
$arr=Array();
if (!$result) return $arr;
while ($row = @mysql_fetch_array($result,$type))
{
$this->check_query_quote($row);
return $row;
}
$this->free($result);
return false;
}
function fetch_subarr(&$result,$keyset='',$valset='')
{
$arr=Array();
if (!$result) return $arr;
if (empty($keyset))
{
return $arr;
}
while ($row = @mysql_fetch_array($result,1))
{
$this->check_query_quote($row);
if (!empty($keyset) and isset($row[$keyset]))
{
$tag=$row[$keyset];
$arr[$tag]=empty($valset) ? $row : $row[$valset];
}
}
$this->free($result);
return $arr;
}
function check_query_quote(&$val)
{
if($this->stripSet) $this->stripVar($val);
}
function stripVar(&$val)
{
if(is_array($val)){
foreach($val as $key => $v)
{
$this->stripVar($val[$key]);
}
}
else if(is_string($val)){
$val=StripSlashes($val);
}
}
function num_rows($result){
return @mysql_num_rows($result);
}
function affected_rows()
{
return @mysql_affected_rows($this->_dblink);
}
function data_seek(&$result,$row_num=0){
return @mysql_data_seek($result,$row_num);
}
function free(&$result){
return @mysql_free_result($result);
}
function last_id(){
$id=@mysql_insert_id($this->_dblink);
return intval($id);
}
function err_code()
{
return @mysql_errno($this->_dblink);
}
function err_msg()
{
return mysql_error($this->_dblink);
}
function close(){
$result = @mysql_close($this->_dblink);
$this->_dblink=0;
}
/* NEW 2010-8-15 */
function getsql($val,$type=0)
{
if($type==1) {
$g='';
if(substr($val,0,1)=='-') $g='-';
$val=preg_replace('/\D/','',$val);
if(strlen($val)==0) $val=0;
return $g.$val;
}
else if($type==2) return doubleval($val);
else if($type==3) return $val;
else{
if ($this->_dblink) return "'" . mysql_real_escape_string($val) . "'";
else return "'" . mysql_escape_string($val) . "'";
}
}
function getstr($val,$type=0) {
return $this->getsql($val,$type);
}
function debug($string,$SQL='')
{
$debug=$this->_conf['debug'];
if ($debug==4) {
die($string.'<HR>'.$SQL);
}
else if ($debug==3) {
print($string.'<HR>'.$SQL);
}
else if ($debug==2) {
print('<!--'.$string.'<HR>'.$SQL.'-->');
}
else if ($debug==1) {
//$code=intval($this->err_code());
return 0;
}
return 0;
}
}
‘叁’ 在PHP中$db=new DB();是什么意思
new 是实例化一个对象
而DB 就是那个对象的名称 也可以叫类名
$db = new DB; // 将DB这个对象实例化后赋予变量$db
$db->query(); // 那么这个query()函数 就是DB类里的一个方法
‘肆’ php 求一个mysqli的db类注释尽可能的多,初学小白
mysqli一个最简单的例子,要深入封装的话可以自己再增加...
其实个人觉得mysqli已经没什么必要封装了.....
<?php
classdb{ //类名
public$con; //定义句柄
public$result; //结果存取
publicfunction__construct($Host,$User,$Pass,$DB){ //构建函数
$this->con=newmysqli($Host,$User,$Pass,$DB); //调用mysqli类
if($this->con->connect_error){ //判断是否有错误,有错误则返回连接错误代号和错误内容
returnarray($this->con->connect_errno,$this->con->connect_error);
}
}
publicfunctionquery($sql,$type=''){ //执行查询,$sql为查询语句,$type为resultmode[MYSQLI_USE_RESULT]OR[MYSQLI_STORE_RESULT]执行成功返回true,否则返回false
$this->result=empty($type)?$this->con->query($sql):$this->con->query($sql,$type);
return!$this->result?false:true;
}
publicfunctioninsertid(){ //必须先进行query才能获得插入或更新的id
return$this->con->insert_id;
}
publicfunctionfetch($n,$t){//获取结果集,$n必选[array][assoc][field_direct][field][fields][object][row][all],$t为$n对应的可选参数,成功返回结果集
$f='fetch_'.$n;
return$this->result->$f($t);
}
publicfunction__destruct(){ //销毁函数
if($this->result)$this->result->close();
if($this->con)$this->con->close();
}
publicfunctionGetError(){ //获取错误
returnarray($this->con->errno,$this->con->error);
}
}
$db=newdb('127.0.0.1','','','test');
if(!$db->query("insertintotb(`time`,`amount`)values('1420085532','300')")){
var_mp($db->GetError());
die();
}
echo$db->insertid(),PHP_EOL;
$db->query('select*fromtb');
while($arr=$db->fetch('array',MYSQLI_NUM)){
echo$arr['0'],'',$arr['1'],'',$arr['2'],'',PHP_EOL;
}
‘伍’ db.php这里面这是什么意思呀,数据库什么的
db.php是一个php页面,不是数据库,而且也不知道用的什么数据库
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。
‘陆’ PHP db类的使用
你的query()有返回值,你可以把代码改成这样再试试
function fn_select($tabl){
return $this->query("select * from $tabl order by id desc");
}
你没有用到自己定义的
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return @mysql_fetch_array($query, $result_type);
}
你试着在后面加上
$row=$db->fetch_array($a,MYSQL_NUM);
print_r($row);
应该就看到结果了
www.365php.net 365PHP培训与交流平台 欢迎光临