In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "using php to operate mysql database class examples to explain", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the use of php to operate mysql database class examples to explain" it!
This paper gives an example of a simple and practical php operation mysql database class. Share it with you for your reference. The details are as follows:
The copy code is as follows:
/ *
This database connection class, he will automatically load sql anti-injection function, filter some sensitive sql query keywords, at the same time, you can also add to determine the nature of the field show table status and show table class to get all the table names of the database. , /
@ ini_set ('mysql.trace_mode','off')
Class mysql
{
Public $dblink
Public $pconnect
Private $search = array ('/ union (s * (/ *. * * /)? s *) + select/i','/ load_file (s * (/ *. * * /)? s *) + (/ ispeak,'/ into (s * (/ *. * /)? s *) + outfile/i')
Private $replace = array ('union select',' load_file (', 'into outfile')
Private $rs
Function _ _ construct ($hostname,$username,$userpwd,$database,$pconnect=false,$charset='utf8')
{
Define ('allowed_htmltags','
')
$this- > pconnect=$pconnect
$this- > dblink=$pconnect?mysql_pconnect ($hostname,$username,$userpwd): mysql_connect ($hostname,$username,$userpwd)
(! $this- > dblink | |! is_resource ($this- > dblink) & & fatal_error ("connect to the database unsuccessfully!")
@ mysql_unbuffered_query ("set names {$charset}")
If ($this- > version () > '5.0.1')
{
@ mysql_unbuffered_query ("set sql_mode ='")
}
@ mysql_select_db ($database) or fatal_error ("can not select table!")
Return $this- > dblink
}
Function query ($sql,$unbuffered=false)
{
/ / echo $sql.'
'
$this- > rs=$unbuffered?mysql_unbuffered_query ($sql,$this- > dblink): mysql_query ($sql,$this- > dblink)
/ / (! $this- > rs | |! is_resource ($this- > rs) & & fatal_error ("execute the query unsuccessfully! error:" .MySQL _ error ())
If (! $this- > rs) fatal_error ('executing sql statement'. $sql.' The following error occurred when: '.mysql_error ())
Return $this- > rs
}
Function fetch_one ($sql)
{
$this- > rs=$this- > query ($sql)
Return dircms_strips tutorial lashes ($this- > filter_pass (mysql_fetch_array ($this- > rs,mysql_assoc)
}
Function get_maxfield ($filed='id',$table) / / gets the maximum value of the $filed field in the $table table
{
Fetch_one ("select {$table}. {$filed} from `{$table} `order by` {$table} `.` {$filed} `desc limit 0Pol 1")
Return $r [$filed]
}
Function fetch_all ($sql)
{
$this- > rs=$this- > query ($sql)
$result=array ()
While ($rows=mysql_fetch_array ($this- > rs,mysql_assoc))
{
$result [] = $rows
}
Mysql_free_result ($this- > rs)
Return dircms_stripslashes ($this- > filter_pass ($result))
}
Function fetch_all_withkey ($sql,$key='id')
{
$this- > rs=$this- > query ($sql)
$result=array ()
While ($rows=mysql_fetch_array ($this- > rs,mysql_assoc))
{
$result [$rows [$key]] = $rows
}
Mysql_free_result ($this- > rs)
Return dircms_stripslashes ($this- > filter_pass ($result))
}
Function last_insert_id ()
{
If (($insertid=mysql_insert_id ($this- > dblink)) > 0) return $insertid
Else / / if the column type of auto_increment is bigint, the value returned by mysql_insert_id () will be incorrect.
{
$result=$this- > fetch_one ('select last_insert_id () as insertid')
Return $result ['insertid']
}
}
Function insert ($tbname,$varray,$replace=false)
{
$varray=$this- > escape ($varray)
$tb_fields=$this- > get_fields ($tbname); / / upgrade to determine whether the field exists
Foreach ($varray as $key = > $value)
{
If (in_array ($key,$tb_fields))
{
$fileds [] = '`.'. $key.' `'
$values [] = is_string ($value)?'. $value.''':$value
}
}
If ($fileds)
{
$fileds=implode (',', $fileds)
$fileds=str_replace ('','', $fileds)
$values=implode (',', $values)
$sql=$replace? "replace into {$tbname} ({$fileds}) values ({$values})": "insert into {$tbname} ({$fileds}) values ({$values})"
$this- > query ($sql,true)
Return $this- > last_insert_id ()
}
Else return false
}
Function update ($tbname, $array, $where ='')
{
$array=$this- > escape ($array)
If ($where)
{
$tb_fields=$this- > get_fields ($tbname); / / add to determine whether the field exists
$sql =''
Foreach ($array as $k = > $v)
{
If (in_array ($kjinghead fields))
{
$k=str_replace (',', $k)
$sql. = ", `$ k` ='$v'"
}
}
$sql = substr ($sql, 1)
If ($sql) $sql = "update `$tbname` set $sql where $where"
Else return true
}
Else
{
$sql = "replace into `$tbname` (`" .implode ('`,`', array_keys ($array)). "`) values ('" .implode (",'", $array). "')"
}
Return $this- > query ($sql,true)
}
Function mysql_delete ($tbname,$idarray,$filedname='id')
{
$idwhere=is_array ($idarray)? implode (',', $idarray): intval ($idarray)
$where=is_array ($idarray)? "{$tbname}. {$filedname} in ({$idwhere})": "{$tbname}. {$filedname} = {$idwhere}"
Return $this- > query ("delete from {$tbname} where {$where}", true)
}
Function get_fields ($table)
{
$fields=array ()
$result=$this- > fetch_all ("show columns from `{$table}`")
Foreach ($result as $val)
{
$fields [] = $val ['field']
}
Return $fields
}
Function get_table_status ($database)
{
$status=array ()
Fetch_all ("show table status from `". $database. "`"); / show table status is similar in nature to show table, but can provide a great deal of information about each table.
Foreach ($r as $v)
{
$status [] = $v
}
Return $status
}
Function get_one_table_status ($table)
{
Return $this- > fetch_one ("show table status like'$table'")
}
Function create_fields ($tbname,$fieldname,$size=0,$type='varchar') / / 2010-5-14 fix it
{
If ($size)
{
$size=strtoupper ($type) = = 'varchar'?$size:8
$this- > query ("alter table `{$tbname}` add `$fieldname` {$type} ({$size}) not null", true)
}
Else $this- > query ("alter table `{$tbname} `add` $fieldname` mediumtext not null", true)
Return true
}
Function get_tables () / / get all table names
{
$tables=array ()
$ringing events-> fetch_all ("show tables")
Foreach ($r as $v)
{
Foreach ($v as $v _)
{
$tables [] = $v _
}
}
Return $tables
}
Function create_model_table ($tbname) / / create a content model table (start: initially only fields contentid int (20) are used for the content table. / / update:2010-5-20 adds `content` mediumtext not null, field by default)
{
If (in_array ($tbname,$this- > get_tables ()) return false; / returns false if the table name already exists
If ($this- > query ("create table `{$tbname}`)
`contentid` mediumint (8) not null
`content` mediumtext not null
Key (`contentid`)
) engine = myisam default charset=utf8 ", true) return true; / returns true if it succeeds
Return false; / failed to return false
}
Function create_table ($tbname) / / create an empty membership model table (initially only fields userid int (20) for membership table, 2010-4-26)
{
If (in_array ($tbname,$this- > get_tables ()) return false
If ($this- > query ("create table `{$tbname}`)
`userid` mediumint (8) not null
Key (`userid`)
) engine = myisam default charset=utf8 ", true)) return true
Return false
}
Function escape ($str) / / filter dangerous characters
{
If (! is_array ($str)) return str_replace (array ('nasty,' r'), array (chr (10), chr (13)), mysql_real_escape_string (preg_replace ($this- > search,$this- > replace, $str), $this- > dblink)
Foreach ($str as $key= > $val) $str [$key] = $this- > escape ($val)
Return $str
}
Function filter_pass ($string, $allowedtags ='', $disabledattributes = array ('onabort',' onactivate', 'onafterprint',' onafterupdate', 'onbeforeactivate',' onbeforecopy', 'onbeforecut',' onbeforedeactivate', 'onbeforeeditfocus',' onbeforepaste', 'onbeforeprint',' onbeforeunload', 'onbeforeupdate',' onblur', 'onbounce',' oncellchange', 'onchange',' onclick', 'oncontextmenu',' oncontrolselect', 'oncopy',' oncut', 'ondataavaible' 'ondatasetchanged', 'ondatasetcomplete',' ondblclick', 'ondeactivate',' ondrag', 'ondragdrop',' ondragend', 'ondragenter',' ondragleave', 'ondragover',' ondragstart', 'ondrop',' onerror', 'onerrorupdate',' onfilterupdate', 'onfinish',' onfocus', 'onfocusin',' onfocusout', 'onhelp',' onkeydown', 'onkeypress',' onkeyup', 'onlayoutcomplete',' onload', 'onlosecapture' 'onmousedown',' onmouseenter', 'onmouseleave',' onmousemove', 'onmoveout',' onmouseo course ver', 'onmouseup',' onmousewheel', 'onmove',' onmoveend', 'onmovestart',' onpaste', 'onpropertychange',' onreadystatechange', 'onreset',' onresize', 'onresizeend',' onresizestart', 'onrowexit',' onrowsdelete', 'onrowsinserted',' onscroll', 'onselect',' onselectionchange', 'onselectstart' 'onstart',' onstop', 'onsubmit',' onunload'))
{
If (is_array ($string))
{
Foreach ($string as $key = > $val) $string [$key] = $this- > filter_pass ($val, allowed_htmltags)
}
Else
{
$string = preg_replace ('/ s ('.implode (' |', $disabledattributes).'). *? ([s >]) /', preg_replace ('/ / ie', "'query (" drop table if exists `{$tbname}` ", true)
}
Function version ()
{
Return mysql_get_server_info ($this- > dblink)
}
}
At this point, I believe that everyone on the "use of php to operate mysql database class examples to explain" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.