數據庫高級操作
(1)獲取報錯信息mysql_error(),mysql_errno()
string mysql_error([resource $link_identifier]);返回上一個mysql函數的錯誤文本,如果沒有出錯則返回空字符串
int mysql_errno([resource $link_identifier]);返回上一個mysql函數的錯誤號碼,如果沒有出錯則返回0
例如:選擇一個不存在的數據庫會出錯,在一個已經存在的數據庫中,操作一個不存在的表也會出錯。
(2)獲取數據庫和表的信息
mysql_list_dbs(),
mysql_db_name(),
mysql_list_tables(),
mysql_tablename()
具體用法:
resource mysql_list_dbs([resource $link_identifier])
string mysql_db_name(resource $result,int $row[,mixed $field])
resource mysql_list_tables(string $databaseName[,resource $link_identifier])
string mysql_tablename(resource $result,int $i)
(3)
▼mysql_free_result()釋放內存
▼mysql_num_rows()判斷結果指針中所指記錄的個數
<?php
#連接數據庫
$link=mysql_connect("localhost","root","root");
//選擇數據庫--不存在的數據庫,出錯
mysql_select_db("noexistentdb");
echo mysql_error()."<br>";
mysql_select_db("rorely");
#查詢不存在的表,出錯
mysql_query("select * from noexistenttable");
echo mysql_error()."<hr>";
#輸出數據庫列表
$db_list=mysql_list_dbs($link);
while($row=mysql_fetch_object($db_list)) echo $row->Database."<br>";
echo"<hr>";
#輸出數據庫列表中所有數據庫的名稱
$i=0;
$count=mysql_num_rows($db_list);
while($i<$count){
echo mysql_db_name($db_list,$i)."<br>";
$i++;
}
echo"<hr>";
#輸出數據庫列表中所有的所有表mysql_list_tables(dbname)
$result=mysql_list_tables("mysql");
if(!$result) {
print "DB error,could not list tables<br>";
print "MySQL error:".mysql_error();
exit;
}
while($row=mysql_fetch_row($result))
print "Table:$row[0].<br>";
mysql_free_result($result);
echo"<hr>";
#輸出數據庫表mysql_tablename(resource)
$result=mysql_list_tables("rorely");
for($i=0;$i<mysql_num_rows($result);$i++){
printf("Table:%s<br>",mysql_tablename($result,$i));
}
mysql_free_result($result);
?>
輸出結果如下:
Unknown database 'noexistentdb'
Table 'rorely.noexistenttable' doesn't exist
information_schema
mysql
rorely
test
information_schema
mysql
rorely
test
Table:columns_priv.
Table:db.
Table:event.
Table:func.
Table:general_log.
Table:help_category.
Table:help_keyword.
Table:help_relation.
Table:help_topic.
Table:host.
Table:ndb_binlog_index.
Table:plugin.
Table:proc.
Table:procs_priv.
Table:servers.
Table:slow_log.
Table:tables_priv.
Table:time_zone.
Table:time_zone_leap_second.
Table:time_zone_name.
Table:time_zone_transition.
Table:time_zone_transition_type.
Table:user.
Table:test
posted on 2009-07-02 09:02
期待明天 閱讀(334)
評論(0) 編輯 收藏 所屬分類:
PHP