1
2
3
4
5
6
7
8
9
10
11

獨立探索

發(fā)布時間:2017-07-20 07:58   發(fā)布人:毛書朋   浏覽次數:6596

1、編寫連接數據庫的類庫,并注明調用方法



  • 侯素玲 2018-01-07 09:05:52
    在您能(néng)夠訪問并處理數據庫中的數據之前,您必須創建到達數據庫的連接。 在 PHP 中,這(zhè)個任務通過(guò) mysql_connect() 函數完成(chéng)。
  • 侯素玲 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); ?>