Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Accessing MySQL from PHP
Search
LucienLee
July 09, 2013
Programming
1
350
Accessing MySQL from PHP
Fundamental PHP & Web Development
LucienLee
July 09, 2013
Tweet
Share
More Decks by LucienLee
See All by LucienLee
SASS & Compass 101
lucienlee
1
290
Use PaaS service to host your web - with pagodabox
lucienlee
0
72
final album
lucienlee
0
66
Simple Album
lucienlee
0
79
DataBase and MySQL
lucienlee
1
230
PHP 101+1:function & form
lucienlee
1
280
PHP 101: flow control
lucienlee
0
320
Start to Build your Web
lucienlee
1
420
既然如此,那我們來hack資本世界吧!
lucienlee
0
150
Other Decks in Programming
See All in Programming
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
370
見た目から始める生産性向上
ikumatadokoro
4
130
Netty Chicago Java User Group 2024-04-17
sullis
0
140
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
100
ゆるい個人開発のススメ
kuroppe1819
10
950
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
350
Folding Cheat Sheet #3
philipschwarz
PRO
0
120
ログラスを支える設計標準について / loglass-design-standards
urmot
10
2.1k
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
430
Elm 0.19.0 Changes
bkuhlmann
0
490
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
180
スクラムチームと認知負荷 - ニフティのスクラムトーク Vol2. / NIFTY Tech Talk #18
niftycorp
PRO
1
120
Featured
See All Featured
The Language of Interfaces
destraynor
151
23k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
124
32k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Building a Scalable Design System with Sketch
lauravandoore
455
32k
Fantastic passwords and where to find them - at NoRuKo
philnash
36
2.5k
Into the Great Unknown - MozCon
thekraken
10
980
Done Done
chrislema
178
15k
[RailsConf 2023] Rails as a piece of cake
palkan
22
3.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.5k
Git: the NoSQL Database
bkeepers
PRO
422
63k
Scaling GitHub
holman
457
140k
Navigating Team Friction
lara
177
13k
Transcript
Accessing MySQL from PHP Fundamental PHP & Web Development lecturer
: Lucien Lee 李柏緯 Lecture 5
2
flow •Connect MySQL Server •Open database •execute SQL •close database
3
connect server 4 Before we can access data in a
database, we must open a connection to the MySQL server.
5
6 mysql_connect mysql_connect(host,username,password); mysql_connect(localhost,‘root’,‘123456’);
SELECT DB choose the db you would like to query
7
8
9 mysql_select_db $link = mysql_connect(localhost,‘root’,‘123456’); mysql_select_db(‘user’, $link);
query execute SQL query BY PHP 10
11
12 mysql_query $sql = “SELECT * FROM table”; $result =
mysql_query($sql,$link);
PHP 5 and older //before query mysql_query('SET NAMES utf8'); 13
fetch result after query, we only get pointer to resource
14
mysql_fetch_array while($row = mysql_fetch_array($result)){ //do something echo $row[‘fieldName’]; echo $row[0];
} 15
mysql_fetch_array while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { //do something echo $row[‘fieldName’]; //echo
$row[0] is wrong } 16
mysql_fetch_assoc while($row = mysql_fetch_assoc($result)) { //do something echo $row[‘fieldName’]; }
17
mysql_fetch_array while($row = mysql_fetch_array($result, MYSQL_NUM)) { //do something //echo $row[‘fieldName’]
is wrong echo $row[0]; } 18
array type •MYSQL_ASSOC •MYSQL_NUM •MYSQL_BOTH 19
remove your pointer we may directly get a specific record
20
mysql_data_seek mysql_data_seek(data,row_num) $row = mysql_fetch_array($result); mysql_data_seek($result,8) $row = mysql_fetch_array($result); //get
9th record 21
mysql_close close Database connect 22
mysql_close mysql_close(resource); 23
more something else 24
mysql_num_rows 25 KNOW HOw many rows selected
mysql_num_rows mysql_num_rows($result); 26
mysql_affected_rows know how many record affected 27
mysql_affected_rows //after update mysql_affected_rows($link_identifier); 28
mysql_fetch_field get field information 29
mysql_fetch_field mysql_fetch_field(data,offset) $meta= mysql_fetch_field($result,1); echo $meta->name; echo $meta->type; 30
? Use SQL to manipulate record 31
INSERT •Insert a new record • INSERT INTO `tableName` (field1,field2,...)
Values (data1,data2...) 32
UPdate •update an exist record •UPDATE `tableName` SET field =
data, ... WHERE condition 33
DELETE •delete an exist record •DELETE FROM `table` WHERE condition
34
You can use better way 35
mysqli 36 mysql improvement
reference 37 http://www.w3schools.com/php/php_ref_mysqli.asp
practice 38 • Make a message board that users can
leave a message containing name(necessary), message and timestamp. • users can view all message and delete them.