ket noi php voi mysql

download ket noi php voi mysql

of 40

  • date post

    04-Apr-2018
  • Category

    Documents

  • view

    221
  • download

    0

Embed Size (px)

Transcript of ket noi php voi mysql

  • 7/30/2019 ket noi php voi mysql

    1/40

    i Hc S Phm Tp. H Ch Minhi Hc S Phm Tp. H Ch Minh

    LP TRNH WEB PHPLP TRNH WEB PHP

    Chng 04: Kt ni PHP vi MySQL

  • 7/30/2019 ket noi php voi mysql

    2/40

    Ni dung

    1. Cc kiu d liu c bn trong MySQL

    2. Cc lnh thng dng trong MySQL3. Kt ni MySQL t PHP

    4. u trnh kt ni vo M S L

    2

    5. Cc bc truy cp CSDL MySQL

  • 7/30/2019 ket noi php voi mysql

    3/40

    11. Cc kiu d liu c bn trong. Cc kiu d liu c bn trong MySQLMySQL

    KiuKiu M tM t

    charchar(length)(length) ti a 255 k t, chiu di cnh =ti a 255 k t, chiu di cnh = lengthlength

    varcharvarchar(length)(length) ti a 255 k t, chiu di ng

  • 7/30/2019 ket noi php voi mysql

    4/40

    KiuKiu M tM t

    enumenum(option1,(option1,

    option2,)option2,)tp hp tnh, ngha ti a 65.535 gi trtp hp tnh, ngha ti a 65.535 gi tr

    -- --

    1. Cc kiu d liu thng dng trong1. Cc kiu d liu thng dng trong

    MySQLMySQL

    4

    timetime hh:mm:sshh:mm:ss

    datetimedatetime yyyyyyyy--mmmm--dd hh:mm:ssdd hh:mm:ss

  • 7/30/2019 ket noi php voi mysql

    5/40

    KiuKiu M tM t

    CREATECREATE to CSDL hoc bngto CSDL hoc bngALTERALTER thay i bng c snthay i bng c sn

    SELECTSELECT chn d liu t bngchn d liu t bng

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    5

    DELETEDELETE xa d liu khi bngxa d liu khi bngDESCRIBEDESCRIBE xem thng tin m t v cu trc bngxem thng tin m t v cu trc bng

    INSERTINSERT INTOINTO ghi gi tr vo bngghi gi tr vo bng

    UPDATEUPDATE cp nht d liu c trong bngcp nht d liu c trong bng

    DROPDROP xa bng hay ton b CSDLxa bng hay ton b CSDL

  • 7/30/2019 ket noi php voi mysql

    6/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    CREATE INDEXCREATE INDEX indexnameindexnameONON tablenametablename(column [ASC|DESC],(column [ASC|DESC],...);...);

    CREATE PROCEDURECREATE PROCEDURE procedurenameprocedurename( [( [parametersparameters] ) BEGIN ...] ) BEGIN ...END;END;

    CREATE TABLECREATE TABLE tablenametablename

    6

    columncolumn datatype [NULL|NOT NULL]datatype [NULL|NOT NULL] [CONSTRAINTS],[CONSTRAINTS],

    columncolumn datatype [NULL|NOT NULL]datatype [NULL|NOT NULL] [CONSTRAINTS],[CONSTRAINTS],

    ......

    ););

    CREATE USERCREATE USER usernameusername[@[@hostnamehostname] [IDENTIFIED BY] [IDENTIFIED BY[PASSWORD] '[PASSWORD] 'passwordpassword'];'];

    CREATE [OR REPLACE] VIEWCREATE [OR REPLACE] VIEW viewnameviewnameAS SELECT ...;AS SELECT ...;

  • 7/30/2019 ket noi php voi mysql

    7/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    ALTERALTER TABLETABLE tablenametablename

    ((ADDADD column datatype [column datatype [NULL|NOTNULL|NOTNULLNULL] [] [CONSTRAINTSCONSTRAINTS],],

    7

    CHANGECHANGE column columns datatype [column columns datatype [NULL|NOTNULL|NOTNULL] [NULL] [CONSTRAINTSCONSTRAINTS],],

    DROPDROP column,column,

    ......););

  • 7/30/2019 ket noi php voi mysql

    8/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    SELECTSELECT columnnamecolumnname, ..., ...

    FROMFROM tablenametablename, ..., ...

    [[WHEREWHERE ...]...]

    [[UNIONUNION ...]...]

    8

    [[GROUPGROUP BYBY ...]...]

    [[HAVINGHAVING ...]...]

    [[ORDERORDER BYBY ...];...];VD:VD: SELECTSELECT ** FROMFROM ketqua;ketqua;

  • 7/30/2019 ket noi php voi mysql

    9/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    DELETE FROMDELETE FROM tablenametablename

    [[WHEREWHERE ...];...];

    9

    VD:VD:DELETE FROMDELETE FROM ketquaketqua WHEREWHERE

    MaSV = K29.103.010MaSV = K29.103.010

  • 7/30/2019 ket noi php voi mysql

    10/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    INSERT INTOINSERT INTO tablenametablename[(columns, ...)][(columns, ...)]

    VALUESVALUES(values, ...);(values, ...);

    INSERT INTOINSERT INTO tablenametablenameVALUESVALUES(value1,(value1,

    10

    , ,, ,

    VD:VD:

    INSERT INTOINSERT INTOketqua (mamon, diem)ketqua (mamon, diem)VALUESVALUES(LTWEB, 10);(LTWEB, 10);

    INSERT INTOINSERT INTOketquaketquaVALUESVALUES(, K29.103.010,(, K29.103.010,

    LTWEB, 10);LTWEB, 10);

  • 7/30/2019 ket noi php voi mysql

    11/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    UPDATEUPDATE tablenametablename

    SETSET columnamecolumname= value, ...= value, ...[[WHEREWHERE ...];...];

    11

    VD:VD:

    UPDATEUPDATE ketquaketqua SETSET diem = 10diem = 10 WHEREWHERE MaSVMaSV

    = K29.103.010= K29.103.010

  • 7/30/2019 ket noi php voi mysql

    12/40

    2. Cc lnh thng dng trong MySQL2. Cc lnh thng dng trong MySQL

    DROP DATABASEDROP DATABASE || INDEXINDEX || PROCEDUREPROCEDURE ||

    TABLETABLE || TRIGGERTRIGGER || USERUSER || VIEWVIEW itemname;itemname;

    12

    Xa bng SINHVIEN:Xa bng SINHVIEN: DROP TABLEDROP TABLE SinhVienSinhVien

    Xa CSDL QLSV:Xa CSDL QLSV: DROP DATABASEDROP DATABASE QLSV;QLSV;

  • 7/30/2019 ket noi php voi mysql

    13/40

    Giao tip dng lnhGiao tip dng lnh

    Kt ni mysql serverKt ni mysql server

    mysql [mysql [--hh hostnamehostname] [] [--PP portnumberportnumber]] --uu usernameusername--pp

    mysqlmysql [-h hostname] [-P portnumber] ----user=user=useruser----

    13

    password=password=passpass

    Nhp lnh sau du nhc lnhNhp lnh sau du nhc lnh mysqlmysql>>

    Mi lnh SQL kt thc bng du ;Mi lnh SQL kt thc bng du ;

  • 7/30/2019 ket noi php voi mysql

    14/40

    Giao tip haGiao tip ha

    Mt s cng c thng dngMt s cng c thng dng

    SQLyog EnterpriseSQLyog EnterprisephpMyAdminphpMyAdmin

    M SQL Quer BrowserM SQL Quer Browser

    14

    MySQL MaestrosMySQL Maestros

    NavicatNavicat

    MySQL ManagerMySQL Manager

  • 7/30/2019 ket noi php voi mysql

    15/40

    3. Kt ni MySQL t PHP3. Kt ni MySQL t PHP

    PHP scriptPHP script

    Th vin chunTh vin chun

    mysqlmysql

    Th vin ci tinTh vin ci tin

    mysqlimysqli

    15

    S dng hmS dng hm

    mysqlimysqli

    S dng lpS dng lp

    mysqlimysqli

    mysqli_stmtmysqli_stmt mysqli_resultmysqli_result

    S dng hmS dng hm

    mysqlmysql

  • 7/30/2019 ket noi php voi mysql

    16/40

    Th vin mysql ci tin trong PHP5Th vin mysql ci tin trong PHP5

    Thit lp trong php.iniThit lp trong php.ini

    extension=php_mysqli.dllextension=php_mysqli.dll

    u imu im

    16

    H tr lp trnh hng i tngH tr lp trnh hng i tng H tr nhn bn v phn tn CSDLH tr nhn bn v phn tn CSDL Nn v m ha d liu trn kt niNn v m ha d liu trn kt ni Ti u hiu nng v mTi u hiu nng v m

  • 7/30/2019 ket noi php voi mysql

    17/40

    4. Quy trnh kt ni vo MySQL4. Quy trnh kt ni vo MySQL

    1.1. M kt ni n CSDLM kt ni n CSDL

    2.2. Chn CSDLChn CSDL3.3. Chn bng m (nu cn)Chn bng m (nu cn)

    17

    ..

    5.5. Dn dpDn dp6.6. ng kt ning kt ni

  • 7/30/2019 ket noi php voi mysql

    18/40

    Bc 1: M kt ni n CSDLBc 1: M kt ni n CSDL

    // OOP mysqli// OOP mysqli

    $mysqli = new$mysqli = new mysqlimysqli('('hostnamehostname', '', 'usernameusername',',

    ''passwordpassword', '', 'dbnamedbname');');

    // m s li// m s li

    18

    $link =$link = mysqli_connectmysqli_connect('('hostnamehostname',',''usernameusername', '', 'passwordpassword', '', 'dbnamedbname');');

  • 7/30/2019 ket noi php voi mysql

    19/40

    Bc 2: Chn CSDLBc 2: Chn CSDL

    // OOP mysqli// OOP mysqli

    $mysqli$mysqli-->select_db>select_db('dbname');('dbname');

    19

    mysqli_select_dbmysqli_select_db($link, 'dbname');($link, 'dbname');

  • 7/30/2019 ket noi php voi mysql

    20/40

    BcBc 33: Chn bng m (nu cn): Chn bng m (nu cn)

    // OOP mysqli// OOP mysqli

    mysqlimysqli-->query>query($link, "SET NAMES ($link, "SET NAMES character setcharacter set")")

    // mysqli// mysqli

    20

    mysqli_querymysqli_query($link, "SET NAMES ($link, "SET NAMES character setcharacter set")")

    VD: SET NAMES UTF8VD: SET NAMES UTF8

  • 7/30/2019 ket noi php voi mysql

    21/40

    Bc 4: X l CSDLBc 4: X l CSDL

    Truy vnTruy vn

    // OOP mysqli// OOP mysqli$result =$result = mysqlimysqli-->query>query(query")(query")

    21

    // mysq// mysq$result =$result = mysqli_querymysqli_query($link, query")($link, query")

  • 7/30/2019 ket noi php voi mysql

    22/40

    Bc 4: X l CSDL (tt)Bc 4: X l CSDL (tt)

    Ly d liu t truy vnLy d liu t truy vn// OOP mysqli// OOP mysqli

    $row =$row = $result$result-->fetch_row()>fetch_row()

    $row =$row = $result$result-->fetch_assoc()>fetch_assoc()$row =$row = $result$result-->fetch_array(>fetch_array(result_typeresult_type))

    // m s li// m s li

    22

    $row =$row = mysqli_fetch_rowmysqli_fetch_row($result)($result)$row =$row = mysqli_fetch_assocmysqli_fetch_assoc($result)($result)$row =$row = mysqli_fetch_arraymysqli_fetch_array($result,($result, result_typeresult_type))

  • 7/30/2019 ket noi php voi mysql

    23/40

    Bc 5: Dn dpBc 5: Dn dp

    // OOP mysqli// OOP mysqli

    $result$result-->close>close()()

    // mysqli// mysqli

    23

    _ __ _

  • 7/30/2019 ket noi php voi mysql

    24/40

    Bc 6: ng kt niBc 6: ng kt ni

    // OOP mysqli// OOP mysqli

    $mysqli$mysqli-->close>close()()

    // mysqli// mysqli

    24

    mysqli_closemysqli_close($link)($link)

  • 7/30/2019 ket noi php voi mysql

    25/40

    5. Cc bc truy cp CSDL MySQL

    1. To kt ni n database server

    2. La chn CSDL