日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

数据库访问 mysql连接库--查询

發布時間:2024/4/14 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库访问 mysql连接库--查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php操作數據庫的三種方法
1.mysql擴展庫?? (最早的);2.mysqli擴展庫;3.pdo

php 數據類型
1.基本數據類型;2.符合數據類型;3.特殊數據類型 null 和資源數據類型

mysql擴展庫和mysql數據庫的區別
1.mysql擴展庫包含操作mysql數據庫的函數(CRUD)
2.數據庫三層結構:sql指令(來自客戶端或者服務器)-->dbms(二次編譯成二進制命令)-->操作數據庫

1.環境搭建
1.1.啟用mysql擴展庫
php.ini ---extension =php_mysql.dll
可以通過 <?php phpinfo();?> 查看使用了那些擴展庫
create table users{???????? //? 大寫執行效率高,不過無所謂
id int? primary key auto_increment,? //unsigned 無符號,范圍大一倍
name varchar(32) not null,? //char varchar的區別
password varchar(64) not null,? //空間按32的倍數來給,標準習慣
email varchar(128) not null,
age tinyint unsigned not null
}

insert into users(name,password,email,age) values('zs',md5('123456'),'123@163.com',30);
insert into users(name,password,email,age) values('偉',md5('123456'),'123@163.com',30);
題外話:md5,不可逆

php代碼

//1.獲取連接
$conn =mysql_connect("127.0.0.1","root","root");
if(!$conn){
die("連接表".mysql_error());
}
//2.選擇數據庫
mysql_select_db("test");
//3.設置操作編碼(建議有)
mysql_query("set names utf8")
//4.sql指令(ddl數據定義語句,dml數據操作語句(增,改刪),dql數據查詢語句(查),dtl數據管理語句 rollback commit)
$sql="select * from users";
$res =mysql_query($sql,$conn); //指定$conn,執行效率高
//var_dump($res);?????????????? //mysql result 資源類型
//5.接受結果,處理
while($row=mysql_fetch_row($res)){? //取出下一行
//第一種取法
?echo"<br/>".row[0]."--".row[1];
//第二種取法
foreach($row as $key=>$val)
{
echo"--$val";
}
echo"<bt/>";
}
//6.釋放資源,關閉連接
mysql_free_result($res);
mysql_close($conn);?? //通常不需要寫,反正不會及時關閉,而且有系統關閉
題外話:cmd命令 netstat -an??? //查看當前的網絡連接

mysql_query($sql,$conn);返回的結果類型
1. dql --- 返回數據庫資源
2.dml---返回bool
mysql_fetch_row($res);??? 返回索引數組 效率最高
mysql_fetch_assoc($res);? 返回關聯數組
mysql_fetch_array($res);? 返回索引數組和關聯數組
mysql_fetch_object($res);? 把一行數據,當做一個對象? $row->email;

mysql command client 的使用

MySql Command Line Client mysql數據庫客戶端-- 使用方法
密碼--回車
show databases;--回車
use test; --回車
show tables; -- 回車??? //查看表結構和所在數據庫
\s???? //查看當前表所在數據庫
drop table users;
show variables like '%char%';?? //不能插入中文數據
set character_set_client=gbk;?? //該客戶端的編碼識別問題
set character_set_results=gbk;
或者使用
set names gbk;??? //取代上面兩行

mysql擴展庫其他操作函數

public function show_table($table_name){

$sql="select * from $table_name";? //"des $table_name";
require_once("sqlhelper.class.php");
$sqlhelper =new sqlhelper();
$res =$sqlhelper->Execute_dql($sql);
echo"<table border=1><tr>";
$rowcount =mysql_affected_rows($conn);
$fieldcount=mysql_num_fields($res);

for($i =0;$i<$fieldcount;$i++){
$field_name=mysql_field_name($res,$i);
echo"<th>$field_name</th>";
}
echo"</tr></table>";

while($row =mysql_fetch_row($res))
echo"</tr>";
for($i=0;$i<$fieldcount;$i++){
echo"<td>$row[$i]</td>";
}
echo"<tr>";
/*//循環獲取列名
while(mysql_fetch_field($res)){
echo"<th>".$field_name.</th>;
}
*/
}

mysql擴展庫--sqlhelper創建

mysql_query($sql,$conn);返回的結果類型
1. dql --- 返回數據庫資源
2.dml---返回bool
if(!$res){
die("operate fail".mysql_error());
}
if(mysql_affected_rows($conn)>0){
echo" operate success";
}
else{
echo"no affected row";
}

創建sqlhelper.class.php? 類

public class sqlhelper {private $host="localhost";private $username="root";private $password ="root";private $db="test";public function sqlhelper(){$conn=mysql_connect($host,$username,$password);if(!$conn){die("connect fail".mysql_error());}mysql_select_db($this->db,$this->conn) or die(mysql_error());mysql_query("set names utf8");}public function Execute_dql($sql){$res=mysql_query($sql,$this->conn);return $res;}mysql_free_result($res);mysql_close($this->conn) or die(mysql_error()); }public function Execute_dml($sql){$res=mysql_query($sql,$this->conn);if(!$res){return 0; //失敗 }else{if(mysql_affected_rows($this->conn)>0){return 1;}else{return 2;}} }

調用sqlhelper使用
$sql="select * from users";
$exe_table =new mysql();
$res =$exe_table->execute_dql($sql);
while($row =mysql_fetch_row($res)){
?? foreach($row as $key=>$value){
?? echo $value;
}
?? echo"<br/>"
}
mysql_free_result($res);

$sql="delete from users";
$exe_table =new mysql();
$exe_table->execute_dml($sql);

mysql擴展庫---字典查詢示例

查詢示例中sql語句的優化
mysql limit 0,1? 指令
select *?? //開銷較大
while 單查開銷比 if 大一點

<?phprequire_once'SqlHelper.class.php';if(isset($_REQUEST['Enname'])){ $en_word=$_REQUEST['Ename']; }else{ echo"input null"; echo"<a href='mainView.php'>return</a>" }$sql="select chword from words where enword='".$en_word."'limit 0,1";/// $sql="select enword from words where chword like '%".$en_word."%' limit 0,1"; if(mysql_num_rows($res)){ while($row=mysql_fetch_assoc($res)){ }else{ } mysql_free_result($res); mysql_close($conn); } ?>

中查英,英查中各一個form,用hidden的值來區分,在同一處理頁里進行處理。

mysqli 特性,sqlhelper封裝類

1.mysql擴展庫和mysqli擴展庫的區別
? 1.1.mysql---面向過程編程
? mysqli---面向對象編程(主流)同時提供面向過程編程(過渡)
? (語法上的特征:mysqli將mysql中使用的參數封裝成相應的對象)
? 1.2.mysqli更安全更高效。
2.

<?php header("Content_type:text/html;charset=utf-8") public class SqlHelper {private static $host="localhost";private static $username="root";private static $password="root";private static $db="test";private mysqli="";public __construct{//self::$host$this->mysqli=new MySQLi(self::$host,self::$username,self::$password,self::$db) //MySQLi中大小無所謂if(!$this->mysqli->conncet_error){die("connect error".$this->mysqli->connect_error);}}public Execute_dql($sql){//des select show explain 返回mysqli result$res=$this->mysqli->query($sql) or die("Query error".$this->mysqli->error);if($res)while($row=$res->fetch-assoic()){foreach($row as $key=>$value)echo"$value";}echo"<br/>"; //$res->free();資源的釋放應該在調用后釋放 //$this->mysqli->close(); }public Execute_dml($sql){$res=$this->mysqli->query($sql) die("Query error".$this->mysqli->error);;if($res){$counts=$this->mysqli->affeced_rows();if($counts>0){//echo"affected rows".$counts;return 0 ; }else{//echo"no rows affected"return 1; }}else{return 2; //echo"opreate fail"; }$this->mysqli->close(); }public Execute_Batch_dql($sql){//批量查詢$res=$this->mysqli->multi_query($sql);if(!$res){echo"operate error".$this->mysqli->error;}else{do{$result=$res->store_result(); //每執行一次取出一個結果集while($row =$result->fetch_row()){foreach($row as $key=>$val){echo"--$val";}echo"</br>";}$result->free();if(!$res->more_results()){break;}}while($res->next_result();)}$this->mysqli->close(); }//批量dmlpublic Execute_Batch_dml($sql){$res=$this->mysqli->multi_query($sql); //批量執行,返回true,false。if(!$res){echo"operate error".$this->mysqli->error;}else{}$this->mysqli->close();} } ?>

mysqli面向過程編程

<?php$mysqli=mysqli_connect("localhost","root","root","db") or die("conncet error".mysqli_connect_error($mysqli));mysqli_query(set names utf8);$sql="select * from users";$res =mysqli_query($mysqli,$sql);if(!$res){echo"operate error".mysqli_error($mysqli);}else{while(mysqli_fetch_row($res)){foreach($row as $key=>$value){echo"--$value";}echo"</br>";}mysqli_free_result($res);mysqli_close($mysqli); } ?>

?

mysqli--批量dql,dml,事務,預處理

批量dql,dml的好處---提高性能速度。
1.批量執行中dml最好不要和dql一起使用,可能會不穩定。
2.批量執行sql語句的不同操作必須用;隔開。

2.sql語句中如果字段為string類型,必須加'';
? 如果字段為數字類型,可以加'',也可以不加'';

<?php requrie_once"SqlHelper.class.php"; $sql="update test set name ='cao'where id=1;"; $sql.="delete from test where id>1;"; $sql.="insert into test (name,age,score) values('piliang',10,100)"$mysqli=new SqlHelper(); $mysqli->Execute_Batch_dml($sql); ?>

3.事務的應用---轉賬(dml)

? 1.acid? 原子性,一致性,隔離性,持久性。
? 2.事務的sql操作
??? 2.1. start transcation;
??? 2.2. savepoint a;
??? 2.3. sql操作;
??? 2.4. rollback to a;或者 commit;
? 3.一旦執行commit就無法再rollback。(持久性)
? 4.事務的php操作

<?phprequire_once("SqlHelper.class.php");$mysqli =new SqlHelper();$mysqli->autocommit(false);$dml1=$mysqli->query("update user set age=age-2 where id=1");$dml2=$mysqli->query("update user set age=age+2 where id=2");if(!$dml1||!$dml2)
  {
$mysqli->rollback();}
  else
  {$mysqli->commit();}$mysqli->close(); ?>

4.預處理
從數據庫請求資源時,造成性能消耗的幾個部分
1.數據庫連接的建立(使用批量處理);2.數據庫編譯語句的消耗(使用預處理)
從數據庫請求資源時的安全問題
1.預處理可以防止sql注入(語法上);2.事務的使用(邏輯上)

小知識:access 本地數據庫,不用監聽,(感覺上有點像xml)
?? cmd?? netstat -an? 查看數據監聽
預處理的應用----向數據庫插入1000條用戶信息(不同參數下的相同操作)

<?php ....$id=1;$name="cao";$age=100;$sql="insert into users values(?,?,?)";$mysqli=new MySQLi("localhost","root","root","test");$mysqli_stmt->prepare($sql);$mysqli_stmt->bindparam("isi",$id,$name,$age); //s代表字符型,i代表數值型$mysqli_stmt->execute() or die("execute error".$mysqli_stmt->error);$id=2;$name="hua";$age=50;$mysqli_stmt->bindparam("isi",$id,$name,$age) ;$mysqli_stmt->execute() or die("execute error".$mysqli_stmt->error);$mysqli_stmt->free(); .... ?>

知識點:預處理中如果發生處理錯誤,預處理會跳過該條數據的處理,繼續執行下一條數據的處理。
??????? 預處理發生在dbms。

預處理stmt---dql,打印表

<?php ....$sql="select id ,name ,age from users where id>?";$mysqli=new MySQLi("localhost","root","root","test");if($mysqli->connect_error){die($mysqli->connect_error);}$mysqli_stmt->prepare($sql);$id=5;$mysqli_stmt->bindparam("i",$id); //參數需要多次綁定$mysqli_stmt->bindresult($id,$name,$age);//此處是按傳址綁定。結果集只需要綁定一次。$mysqli_stmt->execute() or die("execute error".$mysqli_stmt->error);while($mysqli_stmt->fetch()){echo"<br/>--$id--$name--$age";}$mysqli_stmt->free_result();$mysqli_stmt->close(); //關閉預編譯指令$mysqli->close(); .... ?>

小知識:錯誤日志 error_log
sql?? :結構化查詢語言(所有數據庫的基礎)
sql萬能密碼:aa' or 1='1
密碼比對,預處理

mysqli 打印表,

<?php ....echo"has rows".$res->num_rows()."and columns".field_count();echo"<table border='1'><tr>";//從表結構表中獲取對應列。foreach($field=$res->fetch_field();){echo"<th>{$field->name}</th>";}</tr>while($row=$res->fetch_row()){echo"<tr>";foreach($row as $key=>$value){echo"<td>{$value}</td>"}echo"</tr>";}echo"</table>";$res->result(); .... ?>

小知識:pdo提供一個抽象層。

?

轉載于:https://www.cnblogs.com/Watcher/p/3577496.html

總結

以上是生活随笔為你收集整理的数据库访问 mysql连接库--查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。