一直用PDO,PHP中操作MYSQL数据库常用函数还记得不
生活随笔
收集整理的這篇文章主要介紹了
一直用PDO,PHP中操作MYSQL数据库常用函数还记得不
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、mysql_connect()-建立數(shù)據(jù)庫連接
?
格式:?
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
說明:使用該連接必須顯示的關(guān)閉連接?
2、mysql_pconnect()-建立數(shù)據(jù)庫連接?
格式:?
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])?
例:?
$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能連接到Mysql Server");?
說明:使用該連接函數(shù)不需要顯示的關(guān)閉連接,它相當于使用了連接池?
3、mysql_close()-關(guān)閉數(shù)據(jù)庫連接 ?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或數(shù)據(jù)庫不存在");?
echo "你已經(jīng)連接到MyDatabase數(shù)據(jù)庫";?
mysql_close();?
4、mysql_select_db()-選擇數(shù)據(jù)庫 ?
格式:?
boolean mysql_select_db(string db_name [, resource link_id])?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或數(shù)據(jù)庫不存在");?
5、mysql_query()-查詢MySQL ?
格式:?
resource mysql_query (string query, [resource link_id])?
例:?
$linkId = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或者數(shù)據(jù)庫不存在");?
$query = "select * from MyTable";?
$result = mysql_query($query);?
mysql_close();?
說明:若SQL查詢執(zhí)行成功,則返回資源標識符,失敗時返回FALSE。若執(zhí)行更新成功,則返回TRUE,否則返回FALSE?
6、mysql_db_query()-查詢MySQL?
格式:?
resource mysql_db_query(string database, string query [, resource link_id])?
例:?
$linkId = @mysql_connect("localhost", "username", "password") or die("不能連接到MysqlServer");?
$query = "select * from MyTable";?
$result = mysql_db_query("MyDatabase", $query);?
mysql_close();?
說明:為了使代碼清晰,不推薦使用這個函數(shù)調(diào)用?
7、mysql_result()-獲取和顯示數(shù)據(jù) ?
格式:?
mixed mysql_result (resource result_set, int row [, mixed field])?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
for($count=0;$count<=mysql_numrows($result);$count++)?
{?
$c_id = mysql_result($result, 0, "id");?
$c_name = mysql_result($result, 0, "name");?
echo $c_id,$c_name;?
}?
說明:最簡單、也是效率最低的數(shù)據(jù)獲取函數(shù)?
8、mysql_fetch_row()-獲取和顯示數(shù)據(jù) ?
格式:?
array mysql_fetch_row (resource result_set)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while (list($id, $name) = mysql_fetch_row($result)) {?
echo("Name: $name ($id) <br />");?
}?
說明:函數(shù)從result_set中獲取整個數(shù)據(jù)行,將值放在一個索引數(shù)組中。通常會結(jié)使list()函數(shù)使用?
9、mysql_fetch_array()-獲取和顯示數(shù)據(jù)?
格式:?
array mysql_fetch_array (resource result_set [, int result_type])?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {?
$id = $row["id"];?
$name = $row["name"];?
echo "Name: $name ($id) <br />";?
}?
又例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while($row = mysql_fetch_array($result, MYSQL_NUM)) {?
$id = $row[0];?
$name = $row[1];?
echo "Name: $name ($id) <br />";?
}?
說明:?
result_type的值有:?
MYSQL_ASSOC: 字段名表示鍵,字段內(nèi)容為值?
MYSQL_NUM: 數(shù)值索引數(shù)組,操作與mysql_fetch_ros()函數(shù)一樣?
MYSQL_BOTH: 即作為關(guān)聯(lián)數(shù)組又作為數(shù)值索引數(shù)組返回。result_type的默認值。?
10、mysql_fetch_assoc()-獲取和顯示數(shù)據(jù) ?
格式:?
array mysql_fetch_assoc (resource result_set)?
相當于調(diào)用 mysql_fetch_array(resource, MYSQL_ASSOC);?
11、mysql_fetch_object()-獲取和顯示數(shù)據(jù)?
格式:?
object mysql_fetch_object(resource result_set)?
例:?
$query = "select id, name from MyTable order by name";?
while ($row = mysql_fetch_object($result)) {?
$id = $row->id;?
$name = $row->name;?
echo "Name: $name ($id) <br />";?
}?
說明:返回一個對象,在操作上與mysql_fetch_array()相同?
12、mysql_num_rows()-所選擇的記錄的個數(shù) ?
格式:?
int mysql_num_rows(resource result_set)?
例:?
query = "select id, name from MyTable where id > 65";?
$result = mysql_query($query);?
echo "有".mysql_num_rows($result)."條記錄的ID大于65";?
說明:只在確定select查詢所獲取的記錄數(shù)時才有用。?
13、mysql_affected_rows()-受Insert,update,delete影響的記錄的個數(shù) ?
格式:?
int mysql_affected_rows([resource link_id])?
例:?
$query = "update MyTable set name='CheneyFu' where id>=5";?
$result = mysql_query($query);?
echo "ID大于等于5的名稱被更新了的記錄數(shù):".mysql_affected_rows();?
說明:該函數(shù)獲取受INSERT,UPDATE或DELETE更新語句影響的行數(shù)?
14、mysql_list_dbs()-獲取數(shù)據(jù)庫列表信息 ?
格式:?
resource mysql_list_dbs([resource link_id])?
例:?
mysql_connect("localhost", "username", "password");?
$dbs = mysql_list_dbs();?
echo "Databases: <br />";?
while (list($db) = mysql_fetch_rows($dbs)) {?
echo "$db <br />";?
}?
說明:顯示所有數(shù)據(jù)庫名稱?
15、mysql_db_name()-獲取數(shù)據(jù)庫名?
格式:?
string mysql_db_name(resource result_set, integer index)?
說明:該函數(shù)獲取在mysql_list_dbs()所返回result_set中位于指定index索引的數(shù)據(jù)庫名?
16、mysql_list_tables()-獲取數(shù)據(jù)庫表列表 ?
格式:?
resource mysql_list_tables(string database [, resource link_id])?
例:?
mysql_connect("localhost", "username", "password");?
$tables = mysql_list_tables("MyDatabase");?
while (list($table) = mysql_fetch_row($tables)) {?
echo "$table <br />";?
}?
說明:該函數(shù)獲取database中所有表的表名?
17、mysql_tablename()-獲取某個數(shù)據(jù)庫表名 ?
格式:?
string mysql_tablename(resource result_set, integer index)?
例:?
mysql_connect("localhost", "username", "password");?
$tables = mysql_list_tables("MyDatabase");?
$count = -1;?
while (++$count < mysql_numrows($tables)) {?
echo mysql_tablename($tables, $count)."<br />";?
}?
說明:該函數(shù)獲取mysql_list_tables()所返回result_set中位于指定index索引的表名?
18、mysql_fetch_field()-獲取字段信息?
格式:?
object mysql_fetch_field(resource result [, int field_offset])?
例:?
mysql_connect("localhost", "username", "password");?
mysql_select_db("MyDatabase");?
$query = "select * from MyTable";?
$result = mysql_query($query);?
$counts = mysql_num_fields($result);?
for($count = 0; $count < $counts; $count++) {?
$field = mysql_fetch_field($result, $count);?
echo "<p>$field->name $field->type ($field->max_length) </p>";?
}?
說明:?
返回的對象共有12個對象屬性:?
name: 字段名?
table: 字段所在的表?
max_length:字段的最大長度?
not_null: 如果字段不能為null,則為1,否則0?
primary_key: 如果字段為主鍵,則為1,否則0?
unique_key: 如果字段是唯一鍵,則為1, 否則0?
multiple_key: 如果字段為非唯一,則為1,否則0?
numeric: 如果字段為數(shù)值則為1,否則0?
blob: 如果字段為BLOB則為1,否則為0?
type: 字段的數(shù)據(jù)類型?
unsigned: 如果字段為無符號數(shù)則為1,否則為0?
zerofill: 如果字段為“零填充”則為1, 否則為0?
19、mysql_num_fields()-獲取查詢的字段個數(shù) ?
格式:?
integer mysql_num_fields(resource result_set)?
例:?
$query = "select id,name from MyTable order by name";?
$result = mysql_query($query);?
echo "這個查詢的字段數(shù)是:".mysql_num_fields($result)."<br />";?
20、mysql_list_fields()-獲取指定表的所有字段的字段名?
格式:?
resource mysql_list_fields (string database_name, string table_name [, resource link_id])?
例:?
$fields =mysql_list_fields("MyDatabase", "MyTable");?
echo "數(shù)據(jù)庫MyDatabase中表MyTable的字段數(shù): ".mysql_num_fields($fields)."<br />";?
21、mysql_field_flags()-獲取指定的字段選項?
格式:?
string mysql_field_flags (resource result_set, integer field_offset)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
$row=mysql_fetch_wor($row);?
22、mysql_field_len()-獲取指定的字段的最大長度 ?
格式:?
integer mysql_field_len (resource result_set, integer field_offset)?
例:?
$query = "select name from MyTable";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_len($result, 0)."<br />";?
說明:?
如果mysql_field_len($reseult, 0) = 16777215?
那么numer_format(mysql_field_len($result))等于16,777,215?
23、mysql_field_name()-獲取字段名 ?
格式:?
string mysql_field_name (resource result_set, int field_offset)?
例:?
$query = "select id as PKID, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_name($result, 0); // Result: PKID?
24、mysql_field_type()-獲取字段類型?
格式:?
string mysql_field_type (resource result_set, int field_offset)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_type($result, 0); // Result: int?
25、mysql_field_table()-獲取字段所在表名 ?
格式:?
string mysql_field_table (resource result_set, int field_offset)?
例:?
$query = "select id as PKID, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_table($result, 0); // Result: MyTable
格式:?
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
說明:使用該連接必須顯示的關(guān)閉連接?
2、mysql_pconnect()-建立數(shù)據(jù)庫連接?
格式:?
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])?
例:?
$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能連接到Mysql Server");?
說明:使用該連接函數(shù)不需要顯示的關(guān)閉連接,它相當于使用了連接池?
3、mysql_close()-關(guān)閉數(shù)據(jù)庫連接 ?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或數(shù)據(jù)庫不存在");?
echo "你已經(jīng)連接到MyDatabase數(shù)據(jù)庫";?
mysql_close();?
4、mysql_select_db()-選擇數(shù)據(jù)庫 ?
格式:?
boolean mysql_select_db(string db_name [, resource link_id])?
例:?
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或數(shù)據(jù)庫不存在");?
5、mysql_query()-查詢MySQL ?
格式:?
resource mysql_query (string query, [resource link_id])?
例:?
$linkId = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");?
@mysql_select_db("MyDatabase") or die("不能選擇這個數(shù)據(jù)庫,或者數(shù)據(jù)庫不存在");?
$query = "select * from MyTable";?
$result = mysql_query($query);?
mysql_close();?
說明:若SQL查詢執(zhí)行成功,則返回資源標識符,失敗時返回FALSE。若執(zhí)行更新成功,則返回TRUE,否則返回FALSE?
6、mysql_db_query()-查詢MySQL?
格式:?
resource mysql_db_query(string database, string query [, resource link_id])?
例:?
$linkId = @mysql_connect("localhost", "username", "password") or die("不能連接到MysqlServer");?
$query = "select * from MyTable";?
$result = mysql_db_query("MyDatabase", $query);?
mysql_close();?
說明:為了使代碼清晰,不推薦使用這個函數(shù)調(diào)用?
7、mysql_result()-獲取和顯示數(shù)據(jù) ?
格式:?
mixed mysql_result (resource result_set, int row [, mixed field])?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
for($count=0;$count<=mysql_numrows($result);$count++)?
{?
$c_id = mysql_result($result, 0, "id");?
$c_name = mysql_result($result, 0, "name");?
echo $c_id,$c_name;?
}?
說明:最簡單、也是效率最低的數(shù)據(jù)獲取函數(shù)?
8、mysql_fetch_row()-獲取和顯示數(shù)據(jù) ?
格式:?
array mysql_fetch_row (resource result_set)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while (list($id, $name) = mysql_fetch_row($result)) {?
echo("Name: $name ($id) <br />");?
}?
說明:函數(shù)從result_set中獲取整個數(shù)據(jù)行,將值放在一個索引數(shù)組中。通常會結(jié)使list()函數(shù)使用?
9、mysql_fetch_array()-獲取和顯示數(shù)據(jù)?
格式:?
array mysql_fetch_array (resource result_set [, int result_type])?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {?
$id = $row["id"];?
$name = $row["name"];?
echo "Name: $name ($id) <br />";?
}?
又例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
while($row = mysql_fetch_array($result, MYSQL_NUM)) {?
$id = $row[0];?
$name = $row[1];?
echo "Name: $name ($id) <br />";?
}?
說明:?
result_type的值有:?
MYSQL_ASSOC: 字段名表示鍵,字段內(nèi)容為值?
MYSQL_NUM: 數(shù)值索引數(shù)組,操作與mysql_fetch_ros()函數(shù)一樣?
MYSQL_BOTH: 即作為關(guān)聯(lián)數(shù)組又作為數(shù)值索引數(shù)組返回。result_type的默認值。?
10、mysql_fetch_assoc()-獲取和顯示數(shù)據(jù) ?
格式:?
array mysql_fetch_assoc (resource result_set)?
相當于調(diào)用 mysql_fetch_array(resource, MYSQL_ASSOC);?
11、mysql_fetch_object()-獲取和顯示數(shù)據(jù)?
格式:?
object mysql_fetch_object(resource result_set)?
例:?
$query = "select id, name from MyTable order by name";?
while ($row = mysql_fetch_object($result)) {?
$id = $row->id;?
$name = $row->name;?
echo "Name: $name ($id) <br />";?
}?
說明:返回一個對象,在操作上與mysql_fetch_array()相同?
12、mysql_num_rows()-所選擇的記錄的個數(shù) ?
格式:?
int mysql_num_rows(resource result_set)?
例:?
query = "select id, name from MyTable where id > 65";?
$result = mysql_query($query);?
echo "有".mysql_num_rows($result)."條記錄的ID大于65";?
說明:只在確定select查詢所獲取的記錄數(shù)時才有用。?
13、mysql_affected_rows()-受Insert,update,delete影響的記錄的個數(shù) ?
格式:?
int mysql_affected_rows([resource link_id])?
例:?
$query = "update MyTable set name='CheneyFu' where id>=5";?
$result = mysql_query($query);?
echo "ID大于等于5的名稱被更新了的記錄數(shù):".mysql_affected_rows();?
說明:該函數(shù)獲取受INSERT,UPDATE或DELETE更新語句影響的行數(shù)?
14、mysql_list_dbs()-獲取數(shù)據(jù)庫列表信息 ?
格式:?
resource mysql_list_dbs([resource link_id])?
例:?
mysql_connect("localhost", "username", "password");?
$dbs = mysql_list_dbs();?
echo "Databases: <br />";?
while (list($db) = mysql_fetch_rows($dbs)) {?
echo "$db <br />";?
}?
說明:顯示所有數(shù)據(jù)庫名稱?
15、mysql_db_name()-獲取數(shù)據(jù)庫名?
格式:?
string mysql_db_name(resource result_set, integer index)?
說明:該函數(shù)獲取在mysql_list_dbs()所返回result_set中位于指定index索引的數(shù)據(jù)庫名?
16、mysql_list_tables()-獲取數(shù)據(jù)庫表列表 ?
格式:?
resource mysql_list_tables(string database [, resource link_id])?
例:?
mysql_connect("localhost", "username", "password");?
$tables = mysql_list_tables("MyDatabase");?
while (list($table) = mysql_fetch_row($tables)) {?
echo "$table <br />";?
}?
說明:該函數(shù)獲取database中所有表的表名?
17、mysql_tablename()-獲取某個數(shù)據(jù)庫表名 ?
格式:?
string mysql_tablename(resource result_set, integer index)?
例:?
mysql_connect("localhost", "username", "password");?
$tables = mysql_list_tables("MyDatabase");?
$count = -1;?
while (++$count < mysql_numrows($tables)) {?
echo mysql_tablename($tables, $count)."<br />";?
}?
說明:該函數(shù)獲取mysql_list_tables()所返回result_set中位于指定index索引的表名?
18、mysql_fetch_field()-獲取字段信息?
格式:?
object mysql_fetch_field(resource result [, int field_offset])?
例:?
mysql_connect("localhost", "username", "password");?
mysql_select_db("MyDatabase");?
$query = "select * from MyTable";?
$result = mysql_query($query);?
$counts = mysql_num_fields($result);?
for($count = 0; $count < $counts; $count++) {?
$field = mysql_fetch_field($result, $count);?
echo "<p>$field->name $field->type ($field->max_length) </p>";?
}?
說明:?
返回的對象共有12個對象屬性:?
name: 字段名?
table: 字段所在的表?
max_length:字段的最大長度?
not_null: 如果字段不能為null,則為1,否則0?
primary_key: 如果字段為主鍵,則為1,否則0?
unique_key: 如果字段是唯一鍵,則為1, 否則0?
multiple_key: 如果字段為非唯一,則為1,否則0?
numeric: 如果字段為數(shù)值則為1,否則0?
blob: 如果字段為BLOB則為1,否則為0?
type: 字段的數(shù)據(jù)類型?
unsigned: 如果字段為無符號數(shù)則為1,否則為0?
zerofill: 如果字段為“零填充”則為1, 否則為0?
19、mysql_num_fields()-獲取查詢的字段個數(shù) ?
格式:?
integer mysql_num_fields(resource result_set)?
例:?
$query = "select id,name from MyTable order by name";?
$result = mysql_query($query);?
echo "這個查詢的字段數(shù)是:".mysql_num_fields($result)."<br />";?
20、mysql_list_fields()-獲取指定表的所有字段的字段名?
格式:?
resource mysql_list_fields (string database_name, string table_name [, resource link_id])?
例:?
$fields =mysql_list_fields("MyDatabase", "MyTable");?
echo "數(shù)據(jù)庫MyDatabase中表MyTable的字段數(shù): ".mysql_num_fields($fields)."<br />";?
21、mysql_field_flags()-獲取指定的字段選項?
格式:?
string mysql_field_flags (resource result_set, integer field_offset)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
$row=mysql_fetch_wor($row);?
22、mysql_field_len()-獲取指定的字段的最大長度 ?
格式:?
integer mysql_field_len (resource result_set, integer field_offset)?
例:?
$query = "select name from MyTable";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_len($result, 0)."<br />";?
說明:?
如果mysql_field_len($reseult, 0) = 16777215?
那么numer_format(mysql_field_len($result))等于16,777,215?
23、mysql_field_name()-獲取字段名 ?
格式:?
string mysql_field_name (resource result_set, int field_offset)?
例:?
$query = "select id as PKID, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_name($result, 0); // Result: PKID?
24、mysql_field_type()-獲取字段類型?
格式:?
string mysql_field_type (resource result_set, int field_offset)?
例:?
$query = "select id, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_type($result, 0); // Result: int?
25、mysql_field_table()-獲取字段所在表名 ?
格式:?
string mysql_field_table (resource result_set, int field_offset)?
例:?
$query = "select id as PKID, name from MyTable order by name";?
$result = mysql_query($query);?
$row = mysql_fetch_row($result);?
echo mysql_field_table($result, 0); // Result: MyTable
總結(jié)
以上是生活随笔為你收集整理的一直用PDO,PHP中操作MYSQL数据库常用函数还记得不的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis、memcache、mongo
- 下一篇: Yii2使用Cookie的注意事项