Twitter Delicious Facebook Digg Stumbleupon Favorites More

Thứ Tư

Sử dụng CSDL MySQL



mysql_affected_rows 
:
Hổ trợ: PHP 4, PHP 5
Cú pháp: int mysql_affected_rows($link_identifier)
Chức năng: Xác định số dòng được thực hiện bởi INSERT, UPDATE, DELETE trên kết nối $link_identifier
Ví dụ: 
<?php /* this should return the correct numbers of deleted records */ mysql_query('DELETE FROM mytable WHERE id < 10'); printf("Records deleted: %d\n"mysql_affected_rows()); ?> * Ghi chú: nếu $link_identifier = NULL thì hàm sẽ sử dụng kết nối db (mysql_connect) cuối cùng làm tham số
mysql_client_encoding :
Hổ trợ:
Cú pháp: string mysql_client_encoding($link_identifier)
Chức năng: trả về tên character_set của db
Ví dụ:
<?php
$link    
mysql_connect('localhost''mysql_user''mysql_password'); $charset mysql_client_encoding($link);

echo 
"The current character set is: $charset\n"?> 
Kết quả:
The current character set is: latin1
* Ghi chú: nếu $link_identifier = NULL thì hàm sẽ sử dụng kết nối db (mysql_connect) cuối cùng làm tham số

mysql_close:
Hổ trợ
Cú pháp:
Chức năng: Đóng kết nối với database bằng mysql_connect
Ví dụ:
<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
echo 
'Connected successfully'mysql_close($link); ?> 
* Ghi chú: 
- nếu $link_identifier = NULL thì hàm sẽ sử dụng kết nối db (mysql_connect) cuối cùng làm tham số
- khi dùng mysql_pconnect để kết nối db thì việc dùng mysql_close là không cần thiết vì bản thân mysql_pconnect đã tự đóng kết nối sau khi thực hiện hết chương trình

mysql_connect:
Hổ trợ: PHP4, PHP5

Cú pháp:
mysql_connect($hostname,$username,$password)


Chức năng:
Kêt nối đến cơ sở dữ liệu.

Ví dụ:
<?
$host 
"localhost"$username "root"$password ""$conn mysql_connect($hostname,$username,$password);
if(
$conn)
{echo 
"Connected to DATABASE!";}
else
{echo 
"Cannot connect to DATABASE!";} ?> 

hoặc
<?
[quote]
<? 
$host "localhost"$username "root"$password ""mysql_connect($hostname,$username,$password) or die("Cannot connect to DATABASE");
echo 
"Connected to DATABASE"?> [/quote]

Kết quả:
Trả về bool TRUE nếu connect đến CSDL thành công ngược lại FALSE
* Ghi chú:
Để ngắt kết nối đến CSDL sau khi sử dụng, bạn dùng hàm
mysql_close($conn);
mysql_db_name:
Hổ trợ: PHP 4, PHP 5
Cú pháp: string mysql_db_name($result,$row[, mixed $field  ])
Chức năng: xác định tên db từ hàm mysql_list_dbs()
Ví dụ:
<?php
error_reporting
(E_ALL);
$link mysql_connect('dbhost''username''password'); $db_list mysql_list_dbs($link);
$i 0$cnt mysql_num_rows($db_list);
while (
$i $cnt) {
    echo 
mysql_db_name($db_list$i) . "\n";
    
$i++;
?> Kết quả: 
Danh sách tên các db trong mysql server mà username có quyền truy cập
mysql_db_query :
Hổ trợ: PHP 4, PHP 5
Cú pháp: mysql_db_query($database,$query,$link_identifier)
Chức năng: Thực hiện câu query $query trên database $database
Ví dụ:
<?php
$conn 
mysql_connect('mysql_host''mysql_user''mysql_password'); $sql    'SELECT * FROM `table_name` WHERE id = 42'//Chú ý: câu query được thực hiện trực tiếp trên mysql_dbname mà không cần phải dùng mysql_select_db như câu mysql_query thông thường $result mysql_db_query('mysql_dbname',$sql$conn);
while (
$row mysql_fetch_assoc($result)) {
    
print_r($row);
mysql_free_result($result); ?>
mysql_errno:
Hổ trợ: PHP 4, PHP 5
Cú pháp: int mysql_errno([$link_identifier])
Chức năng: Mã lỗi của câu truy vấn mysql
Ví dụ:
<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");

if (!
mysql_select_db("nonexistentdb"$link)) {
    echo 
'Mã lỗi: '.mysql_errno($link);
}
mysql_select_db("kossu"$link);
if (!
mysql_query("SELECT * FROM nonexistenttable"$link)) {
    echo 
'Mã lỗi: '.mysql_errno($link);
?> Kết quả:
Mã lỗi: 1049
Mã lỗi: 1146
* Ghi chú: hàm này trả về 0 nếu câu truy vấn không có lỗi

mysql_error:
Hổ trợ: PHP 4, PHP 5
Cú pháp: string mysql_error([$link_identifier])
Chức năng: trả về câu thông báo lỗi của mysql server 
Ví dụ:
<?php
$link 
mysql_connect("localhost""mysql_user""mysql_password");
mysql_select_db("nonexistentdb"$link);
echo 
mysql_error($link). "\n";
mysql_select_db("kossu"$link); mysql_query("SELECT * FROM nonexistenttable"$link);
echo  
mysql_error($link) . "\n"?> 
Kết quả:
Unknown database 'nonexistentdb'
Table 'kossu.nonexistenttable' doesn't exist
* Ghi chú: hàm này sẽ trả về 1 chuỗi rỗng ('') nếu câu truy vấn không có lỗi

mysql_fetch_array:
Hổ trợ: PHP 4,PHP 5
Cú pháp:mysql_fetch_array($result [,$type= MYSQL_BOTH])
Chức năng: Lấy 1 dòng dữ liệu của câu truy vấn và trả về dưới dạng 1 mảng associative, numeric, hoặc cả 2 dạng 
Ví dụ:
<?php
mysql_connect
("localhost""mysql_user""mysql_password") or
    die(
"Could not connect: " mysql_error()); mysql_select_db("mydb");
$result mysql_query("SELECT id, name FROM mytable");

while (
$row mysql_fetch_array($resultMYSQL_NUM)) {
    
printf("ID: %s  Name: %s"$row[0], $row[1]);  
}
mysql_free_result($result); ?> 

* Ghi chú: 
Giá trị của $type:
MYSQL_ASSOC: hàm trả về 1 mảng associative
MYSQL_NUM: hàm trả về 1 mảng numeric
MYSQL_BOTH: hàm trả về 1 mảng bao gồm cả 2 mảng trên, đây là giá trị mặc định của hàm mysql_fetch_array
mysql_fetch_assoc:
Hổ trợ: PHP 4,PHP 5
Cú pháp:mysql_fetch_assoc($result)
Chức năng: Lấy 1 dòng dữ liệu của câu truy vấn và trả về dưới dạng 1 mảng associative
Ví dụ:
<?php
mysql_connect
("localhost""mysql_user""mysql_password") or
    die(
"Could not connect: " mysql_error()); mysql_select_db("mydb");
$result mysql_query("SELECT id, name FROM mytable");

while (
$row mysql_fetch_assoc($result)) {
    
printf("ID: %s  Name: %s"$row['id'], $row['name']);  
}
mysql_free_result($result); ?>
mysql_fetch_field:
Hổ trợ: PHP 4, PHP 5
Cú pháp:object mysql_fetch_field($result [,$field_offset])
Chức năng: Trả về 1 object chứa thông tin của  1 field , bao gồm các thông tin:
    * name - tên field
    * table - tên của table 
    * def - giá trị mặc định
    * max_length - chiều dài tối đa của field
    * not_null - 1 nếu có ràng buộc NOT NULL
    * primary_key - 1 nếu field này là khóa chính
    * unique_key - 1 nếu field này có thuộc tính UNIQUE
    * multiple_key - 1 nếu field này không cần duy nhất
    * numeric - 1 nếu field có kiểu dữ liệu số ( INT, TINYINT,...)
    * blob - 1 nếu field có kiểu dữ liệu BLOB
    * type - kiểu dữ liệu của field
    * unsigned - 1 nếu có thuộc tính UNSIGNED
    * zerofill - 1 nếu có thuộc tính Zero Fill

Ví dụ:
<?php
$conn 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$conn) {
    die(
'Could not connect: ' mysql_error());
mysql_select_db('database'); $result mysql_query('select * from table');
if (!
$result) {
    die(
'Query failed: ' mysql_error());
/* get column metadata */ $i 0;
while (
$i mysql_num_fields($result)) {
    echo 
"Information for column $i:<br />\n";
    
$meta mysql_fetch_field($result$i);
    if (!
$meta) {
        echo 
"No information available<br />\n";
    }
    echo 
"<pre>
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
default:      $meta->def
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
</pre>"
;
    
$i++;
mysql_free_result($result); ?>
mysql_list_tables:
Hổ trợ: PHP 4, PHP 5
Cú pháp: resource mysql_list_tables($database,$link_identifier)
Chức năng: Trả về 1 danh sách các tên các table trong database: $database
Ví dụ:
<?php
mysql_connect
("localhost","root",'123'); $dbname 'mydbname'$sql mysql_list_tables($dbname);
while(
$rs mysql_fetch_assoc($sql)) echo $rs['Tables_in_'.$dbname]."\n"?> Kết quả:
table1
table2
table3
* Ghi chú: Sử dụng hàm này tương đương với sử dụng câu query: SHOW TABLES FROM $dbname
mysql_select_db:
Hổ trợ: PHP 4, PHP 5
Cú pháp: mysql_select_db($database_name,$link_identifier)
Chức năng: chọn database của mysql server cho $link_identifier và mọi câu query trên $link_identifier đều thực hiện trên db này
Ví dụ:
<?php

$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Not connected : ' mysql_error());
}
// make foo the current db $db_selected mysql_select_db('foo'$link);
if (!
$db_selected) {
    die (
'Can\'t use foo : ' mysql_error());
?> * Ghi chú:
- mỗi server mysql có thể có nhièu db, nên dùng hàm này để xác định db cần thực hiện
- nếu $link_identifier = NULL thì hàm sẽ sử dụng kết nối db (mysql_connect) cuối cùng làm tham số 

Nguồn : thuviensv.com

0 nhận xét:

Đăng nhận xét

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates