PHP連線資料庫

2021-09-09 07:33:03 字數 2543 閱讀 2791

2.mysqli

(2)查詢

(3)記憶體**

(4)事務

(5)預處理

關閉資料庫:$dbh = null;

try

/************ fetch ************/

$stmt

=$dbh

->

query

("select * from yyfs");

while

($result

=$stmt

->

fetch

(pdo::

fetch_num))

/************ fetchall ************/

$stmt

=$dbh

->

query

("select * from yyfs");

$result

=$stmt

->

fetchall

(pdo::

fetch_assoc

)foreach

($result

as$row

)$dbh

=null;}

catch

(pdoexception $e

)

//讓pdo物件「進入」異常模式,以處理出錯資訊

$pdo

->

setattribute

(pdo::

attr_errmode

,pdo::

errmode_exception);

//異常模式處理的**

trycatch

(exception $e

)

$stmt

=$dbh

->

prepare

('select * from yyfs where job = :job');

$job

='php工程師'

;/****** 第一種 ******/

$stmt

->

bindparam

('job'

,$job);

$stmt

->

execute()

;/****** 第二種 ******/

$stmt

->

execute([

'job'

=>

'php工程師'])

;

$conn

=new

mysqli

('localhost'

,'root'

,'root'

,'yyfs');

if(mysqli_connect_errno

($conn))

echo

'連線失敗:'

,mysqli_connect_error

($conn);

//開啟事務

$conn

->

autocommit

(false);

$result

=$conn

->

query

('delete from name where id = ***');

$conn

->

rollback()

;//回滾

echo

'刪除了記錄:'

,$conn

->

affected_rows

,'條'

;//全部提交

$conn

->

commit()

;//結束事務

$conn

->

autocommit

(true

);

//用?號來代替變數佔位

$stmt

=$conn

->

prepare

('select username,password from member where username = ? and password = ?');

//繫結變數,並預處理全為字串

$stmt

->

bind_param

('ss'

,$username

,$password);

$username

='yyfs'

;$password

='root'

;$stmt

->

execute()

;//執行

//繫結結果集

$stmt

->

bind_result($u

,$p);

while

($stmt

->

fetch()

)

php連線資料庫

create table message id tinyint 1 not null auto increment,user varchar 25 not null,title varchar 50 not null,content tinytext not null,lastdate date n...

php連線資料庫

天貓內部優惠券 設定資料庫變數 db host localhost 資料庫主機名稱,一般都為localhost db user root 資料庫使用者帳號,根據個人情況而定 db passw 資料庫使用者密碼,根據個人情況而定 db name test 資料庫具體名稱,以剛才建立的資料庫為準 連線資...

PHP連線資料庫

php連線資料庫函式 mysql connect 開啟mysql連線 mysql select db 開啟乙個資料庫 和or die 隱藏錯誤和條件顯示 mysql connect 主機 使用者名稱 密碼 mysql select db 開啟資料庫 鏈結標示符 如果不是特宣告連線標示符,則預設為是上...