1
2
3
4
5
6
7
8
9
10
11

独立探索

发布时间:2017-07-20 07:58   发布人:毛书朋   浏览次数:5577

1、编写连接数据库的类库,并注明调用方法



  • 侯素玲 2018-01-07 09:05:52
    在您能够访问并处理数据库中的数据之前,您必须创建到达数据库的连接。 在 PHP 中,这个任务通过 mysql_connect() 函数完成。
  • 侯素玲 2018-01-07 09:06:32
    语法:mysql_connect(servername,username,password);
  • 侯素玲 2018-01-07 09:07:33
    例子: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code ?>
  • 孙淑平 2018-01-07 16:18:02
    <?php class C_MySQL{ var $link_id; var $result; public function __construct() { $test=true; if($test) { $host="127.0.0.1"; $name="root"; $pass="root"; $db="wxads"; $code="utf-8"; } else { $host="114.215.110.13"; $name="root"; $pass="XXXXXX"; $db="wxtest"; } define('CLIENT_MULTI_RESULTS', 131072); $conn = mysql_connect ($host,$name,$pass,1,CLIENT_MULTI_RESULTS) or die ( "连接失败" ); mysql_select_db ($db, $conn ); mysql_query ( "SET NAMES '".$code."'", $conn); $this->link_id = $conn; } function __destruct () { mysql_close($this->link_id); } private function fetcharray() { $data = array (); while ( $row = mysql_fetch_array ( $this->result ) ) { array_push ( $data, $row ); } mysql_free_result ( $this->result ); return $data; } public function EXE_select($sql) { $this->result = mysql_query ( $sql, $this->link_id ); if($this->result) return $this->fetcharray(); else return mysql_error(); } public function EXE_delete($sql) { return $this->EXE_update($sql); } public function EXE_update($sql) { $tmp=mysql_query ( $sql, $this->link_id ); if($tmp) return mysql_affected_rows($this->link_id); else return mysql_error(); } public function EXE_insert($sql) { $tmp=mysql_query ( $sql, $this->link_id ); if($tmp) return mysql_insert_id($this->link_id); else return mysql_error(); } } function D($sql) { $db=new C_MySQL(); switch(strtolower(substr($sql,0,6))) { case 'select': return $res=$db->EXE_select($sql); break; case 'delete': return $res=$db->EXE_delete($sql); break; case 'update': return $res=$db->EXE_update($sql); break; case 'insert': return $res=$db->EXE_insert($sql); break; } } $sql="delete from `user` where WX_ID='XXXXXXXXXXXXXX'"; $res=D($sql); echo ($res); ?>