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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
LucienLee
July 09, 2013
Programming
1
370
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
300
Use PaaS service to host your web - with pagodabox
lucienlee
0
82
final album
lucienlee
0
73
Simple Album
lucienlee
0
96
DataBase and MySQL
lucienlee
1
230
PHP 101+1:function & form
lucienlee
1
290
PHP 101: flow control
lucienlee
0
330
Start to Build your Web
lucienlee
1
430
既然如此,那我們來hack資本世界吧!
lucienlee
0
160
Other Decks in Programming
See All in Programming
CSC307 Lecture 04
javiergs
PRO
0
650
CSC307 Lecture 06
javiergs
PRO
0
680
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
640
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.4k
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
680
Grafana:建立系統全知視角的捷徑
blueswen
0
320
高速開発のためのコード整理術
sutetotanuki
1
380
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
250
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
510
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
140
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
180
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.5k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
54
The Curse of the Amulet
leimatthew05
1
8.2k
Rails Girls Zürich Keynote
gr2m
96
14k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
66
36k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Optimizing for Happiness
mojombo
379
71k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
220
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.6k
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.