ฟังก์ชันของภาษา PHP ที่สนับสนุน MySQL

Download Report

Transcript ฟังก์ชันของภาษา PHP ที่สนับสนุน MySQL

การเขียนโปรแกรม PHP เชื่อมต่ อกับ MySQL
วิทยา กรระสี (วท.บ. วิทยาการคอมพิวเตอร์)
[email protected]
1
ฟั งก์ ชันของภาษา PHP ที่ใช้ ตดิ ต่ อกับ MySQL
PHP มีฟังก์ชนั ที่ใช้สาหรับการติดต่อกับฐานข้อมูลโดยตรง ตามระบบ
ฐานข้อมูลที่แตกต่างกัน เช่น MySQL, Qracle
2
ตัวอย่าง โปรแกรม menu.php3
<?php require('common.inc'); ?>
<?php affy_header('Administrative Menu') ?>
<h1>Administrative Menu</h1>
<ol>
<li><a href="connect.php3">Create Database Connection</a></li>
</ol>
<?php affy_footer() ?>
3
รายละเอียดแฟ้ ม common.inc
<?php
function affy_footer() {
echo '</body></html>';
}
function affy_header($title) {
echo '<html><head><title>';
echo "$title";
echo '</title></head><body>';
}
function affy_message($msg) {
echo '<table>';
echo '<tr><td>';
echo "$msg";
echo '</td></tr>';
echo '</table>';
}
?>
4
การเชื่อมต่อกับฐานข้อมูล MySQL
mysql_connect เป็ นฟังก์ชนั ที่ใช้ในการการเชื่อมต่อกับฐานข้อมูล
MySQL มีรูปแบบดังนี้
int mysql_connect( string hostname, string username, string password);
- hostname คือชื่อ host ที่ติดตัง้ MySQL
- username คือชื่อของผู้ใช้ ที่มีสทิ ธ์เข้ าใช้ MySQL
- password คือรหัสผ่านของผู้ใช้
5
connect.php
<?php
// include the definition of the
// affy_message function.
require('common.inc');
?>
<?php
// Try to make the connection. The @ notation is used to suppress
// error message generated by the connect function. Instead,
// the $id_link variable is checked.
//
$id_link = mysql_connect('localhost', 'phpuser', 'phpuser');
// If the connection failed, the $id_link variable is false,
// and the condition becomes true.
//
6
connect.php (cont.)
if (! $id_link) {
// Display a formatted message.
affy_message(
"The connection to the local database has failed. Please enter a
username and password so a connection can be made."
);
?>
<!-- Display a form to gather username and password info -->
<form action="connect.php3" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" value="root"></td>
</tr>
7
connect.php (cont.)
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Connect to Database">
</td>
</tr>
</table>
</form>
<?php } ?>
8
ผลลัพธ์ของโปรแกรม
9
Accessing HTML Form Information
PHP จะนาค่าที่รับจาก field ในแบบฟอร์มไปเก็บในอาร์เรย์ที่ชื่อ
$HTTP_POST_VARS หรื อ $HTTP_GET_VARS ขึ้นกับวิธีการส่ ง
ข้อมูล
จากตัวอย่างข้างต้นอาร์เรย์จะมี 2 สมาชิกคือ username และ password
ดังนั้นค่าที่เก็บในอาร์เรย์คือ $HTTP_POST_VARS[‘username’] และ
$HTTP_POST_VARS[‘password’]
สามารถเรี ยกใช้จาก URL ได้คือ
http://hostname/connect.php3?username=root&password=password
10
เขียนโปรแกรมรองรับทั้ง POSTและ GET
$arr_request = array() ;
if (count ($HTTP_GET_VARS)) {
while(list($key,$value) = each($HTTP_GET_VARS)) {
&arr_request[strtolower($key)]=$value;
}
}
if (count ($HTTP_POST_VARS)) {
while(list($key,$value) = each($HTTP_POST_VARS)) {
&arr_request[strtolower($key)]=$value;
}
}
11
การใช้งาน
นาส่ วนของโปรแกมไปไว้ใน coommon.inc
เรี ยกใช้โดย $arr_request[‘username’]
12
Using HTML FORM information
สร้างการติดต่อกับฐานข้อมูล
$id_link = @mysql_connect('localhost', ’seree', ’sereec');
หรื อ
$id_link = @mysql_connect('localhost', $username, $password);
initialization
if (count($arr_request)) {
$username= $arr_request[‘username’] ;
$password= $arr_request[‘password’] ;
}
else {
$username= ‘username’ ;
$password= ‘password’ ;
}
13
Connect3.php (revised)
<?php require('common.inc'); ?>
<?php
if (count($arr_request)) {
$username = $arr_request['username'];
$password = $arr_request['password'];
}
else {
$username = 'codebits';
$password = 'codebits';
}
?>
<?php
$id_link = @mysql_connect('localhost', $username, $password);
14
Connect3.php (revised)
if (! $id_link) {
affy_message(
"The connection to the local database has failed. Please enter a
username and password so a connection can be made."
);
?>
<form action="connect.php3" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" value="root"></td>
</tr>
15
Connect3.php (revised)
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Connect to Database">
</td>
</tr>
</table>
</form>
<?php exit; } ?>
<p>The connection was successful!</p>
16
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
Mysql_affected_rows- เป็ นการหาจานวนแถวที่มีในการใช้ ครัง้ สุดท้ าย
Int mysql_affected_rows(int link_identifier)
Mysql_close- เป็ นการปิ ดการเชื่อมต่อกับ MySQL
Int mysql_close(int link_identfier);
Mysql_connect-เป็ นการเปิ ดการเชื่อมต่อกับ MySQL Server
Int mysql_connect( string hostname, string username, string password);
17
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
mysql_create_db – เป็ นการสร้ างฐานข้ อมูลที่เป็ น MySQL
Int mysql_create_db(string database name);
mysql_data_seek – เป็ นการย้ าย pointer ที่ชี ้ไปยังแถวภายใน
Int mysql_data_seek(int result_identifier, int row_number);
mysql_dbname – จะเป็ นการหาชื่อของ MySQL database
String mysql_dbname(string result, int I);
18
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
mysql_db_query – เป็ นการส่ง MySQL Query
Int mysql_db_query(string database, string query, int link_identifier);
mysql_drop_db – เป็ นการ drop หรื อลบฐานข้ อมูลที่เป็ น MySQL
Int Mysql_drop_db(string database_name, int link_identifier);
mysql_errno – คืนค่าหมายเลขของ error ที่เรี ยก mysql ครัง้ สุดท้ าย
Int nysql_errno();
19
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
mysql_error – เป็ นการคืนข้ อความที่เป็ น error
String mysql_error();
mysql_fetch_array – ทาการ fetch แถวมาเป็ นแบบ array
Int mysql_fetch_array(int result);
mysql_fetch_field – เป็ นการหาข้ อมูลของ field นัน้
object mysql_fetch_field(int result, int field_offset);
20
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
mysql_detch_lengths – เป็ นการหาขนาดความสูงสุดของ data ที่อยูใ่ น
คอลัมน์
Int mysql_detch_lengths(int result);
mysql_fetch_object – เป็ นการ fetch แถวให้ เป็ น object
Int mysql_fetch_object(int result);
mysql_fetch_row – เป็ นการหาแถวโดยการกาหนดผ่าน array
Array mysql_fetch_row(int result);
21
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
mysql_fetch_name – เป็ นการหาชื่อ field
String mysql_fetch_name(string result, int I);
mysql_field_seek – เป็ นการกาหนดค่า offset ของ field
Int mysql_field_seek(int result, int field_offset);
mysql_field_table – เป็ นการใส่ชื่อตารางให้ field
Int Mysql_field_table(void);
22
ฟั งก์ ชันของภาษา PHP ที่สนับสนุน MySQL
Mysql_field_type – เป็ นการใส่ชนิดของ field
String Mysql_field_type(string result, int i);
mysql_field_flags – เป็ นการหาค่า field flags
String mysql_field_flags(string result, int i);
23