Upload
birhaneselasie-abebe
View
220
Download
0
Embed Size (px)
Citation preview
7/23/2019 PHP Chapter 10
1/27
7/23/2019 PHP Chapter 10
2/27
mysqli MySqi "improved# API
$ery similar to mysql with some
improvements PD!
PHP Data !b%ects
All the three are e&tremely similar In this chapter we will use the mysqli
as our API or PHP
7/23/2019 PHP Chapter 10
3/27
'e will use the procedural version omysqli API
(here are ) basic steps or databaseinteraction in PHP*+ ,reate a database connection
-+ Perorm database query
.+ /se returned data0i any12+ 3elease the returned data
)+ ,lose the database connection
,onnectin to MySql withPHP
7/23/2019 PHP Chapter 10
4/27
Steps * and ) are used only once per PHPscripts
Steps - to 2 may be repeated many times
'e now ocus on steps * and ) 0openinthe connection and closin theconnection1
And the PHP unctions that we use to dothat which are part o the PHP API mysqliare the ollowin
7/23/2019 PHP Chapter 10
5/27
mysqli4connect01
mysqli4connect4errno01mysqli4connect4error01
mysqli4close01
7/23/2019 PHP Chapter 10
6/27
56php
77*+ ,reate a databse connection
8dbhost 9 :localhost:;
8dbuser 9 :root:;8dbpass 9 ::;
8dbname 9 :widet4crop:;
8connection 9 mysqli4connect08dbhost
7/23/2019 PHP Chapter 10
7/27
'e use the ollowin three mysqli API unctionsmysqli4query01mysqli4etch4row01mysqli4ree4result01
77-+ Perorm database query8query 9 :SL,( B C3!M sub%ects:;8result 9 mysqli4query08connection
7/23/2019 PHP Chapter 10
8/27
77.+ /se returned data 0i any1
while08row 9 mysqli4etch4row08result11=
77output data rom each row
var4dump08row1;echo :5hr@:;
?
772+ 3elease returned data
mysqli4ree4result08result1;
7/23/2019 PHP Chapter 10
9/27
'orEin with 3etrieved Data (he ollowin three ways can be used to retrieve data rom
query results*+ mysqli4etch4row01 > F it brins bacE a row o data and
assins it to a standard array+F the Eeys are inteers
-+ mysqli4etch4assoc01> F the result are returned in anassociative array
F the Eeys are column names
F it is slower.+ mysqli4etch4array01> F the results can be returned instandard array usin proper variable names maEe
the code more readable+ &> use8sub%ects instead o 8row
7/23/2019 PHP Chapter 10
17/27
,reatin 3ecords with PHPSuccess Failure
SL,( 3esource Calse
IS3( (rue Calse
/PDA( (rue Calse
DL( (rue Calse
7/23/2019 PHP Chapter 10
18/27
(o insert data into a database ocuson only steps *
7/23/2019 PHP Chapter 10
19/27
&le
56php
77oten these are orm values in 84P!S(
8menu4name 9 "dit me#;
8position 9 2;
8visible 9 *;
77 -+ perorm database query8query 9 "IS3( I(! sub%ects 0menu4name 5input type9 :te&t: name9:position: siQe9 :.R:@5br@
nter visible> 5input type9 :te&t: name9 :visible:siQe9 :.R:@5br@
5input type9:submit: value9:Send:@
57orm@ 57body@ 57html@
7/23/2019 PHP Chapter 10
22/27
8menu4name 9 84P!S(Kmenu4name;
8position 9 84P!S(Kposition;
8visible 9 84P!S(Kvisible;
8query 9 :IS3( I(! sub%ects 0menu4name< position Similar to insertion ocus on *
7/23/2019 PHP Chapter 10
24/27
7/23/2019 PHP Chapter 10
25/27
7/23/2019 PHP Chapter 10
26/27
77,orrection
77-+ perorm database query
77oten these are orm values in 84P!S(
8id9*RR;8menu4name 9:Delete me:;
8query 9 :/PDA( sub%ects S(menu4name9=8menu4name? 'H3 id9=8id?:;
8result 9 mysqli4query08connection
7/23/2019 PHP Chapter 10
27/27
Deletin a record> Similar to updatin with a ew chanes
77-+ perorm database query 77oten these are orm values in 84P!S(
8id9;
8menu4name 9:Delete me:;
8query 9 "DL( C3!M sub%ects 'H3 id9=8id?:;
8result 9 mysqli4query08connection