建立資料庫連線 mysql_pconnect() resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
範例:$conn = @mysql_pcpconnect(" localhost", "username", "password") or dir("不能連接到Mysql Server"); 關閉資料庫連線
$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server");@mysql_select_db("MyDatabase") or die("不能選擇這個數據庫,或數據庫不存在"); echo "你已經連接到MyDatabase數據庫";mysql_close();
mysql_select_db()> relink mysql_select_giding 🎜>$conn = @mysql_connect("localhost", "username", "password") or die("不能連接到Mysql Server"); @mysql_select_db("MyDatabase") or die("不能選擇這個資料庫,或數據庫不存在");
$linkId = @mysql_connect("localhost", "username", "password") or die ("不能連接到Mysql Server");@mysql_select_db("MyDatabase") or die("不能選擇這個資料庫,或是資料庫不存在");$query = "select * from MyTable";$result = mysql_query($query);mysql_close(); 若SQL查詢執行成功,則傳回資源標識符,失敗時傳回FALSE。若執行更新成功,則回傳TRUE,否則回傳FALSE
resource mysql_db_query(string database, string query [ @mysql_connect("localhost", "username", "password") or die("不能連線到MysqlServer");$query = "select * from MyTable";$result = mysql_db_query("MyDatabase" , $query);mysql_close();為了讓程式碼清晰,不建議使用這個函數呼叫 複製程式碼
mysql_result()mixed mysql_result (resource resultset, introwfield] $query = "select id, name from MyTable order by name"; $result = mysql_query($query);$c_id = mysql_result($result, 0, "id"); mysql_fetch_row() = "select id, name from MyTable order by name";$result = mysql_query($query);while (list($id, $name) = mysql_fetch_row($result)) { echo ("Name: $name ($id) $c_name = mysql_result($result, 0, "name");
取得並顯示資料
");}函數從result_set取得整個資料行,將值放在一個索引陣列中。通常會結使list()函數使用複製程式碼 取得並顯示資料
複製程式碼 取得並顯示數據複製程式碼 取得並顯示資料mysql_fetch_object()object mysql_fetch_object(resource result_set) $query = "select id, name from MyTable order by name"; > $id = $row->id; $name = $row->name;
echo "Name: $name ($id) 所選擇的記錄
";
mysql_num_rows() int mysql_num_rows(resource result_set)#query = "select id, name from MyTable where id > 65";echo "有".mysql_num_rows($result)."筆記錄的ID大於65";
$query = "update MyTable set name="CheneyFu" where id>=5";$ 複製程式碼
";while (list($db) = mysql_fetch_rows($dbs)) { echo "$db
"; }
";}此函數取得database中所有表的表名 ";}函數取得mysql_list_tables()所傳回result_set中位於指定index索引的表名 複製程式碼 取得欄位資訊 $field->name $field->type ($field->max_length) ";複製程式碼傳回的物件共有12個物件屬性: name: 欄位名 table: 欄位所在的表 max_length: 欄位的最大長度 not_null: 如果欄位不能為null,則為1,否則0 primary_key: 若欄位為主鍵,則為1,否則0 unique_key: 若欄位是唯一鍵,則為1, 否則0 multiple_key: 若欄位為非唯一,則為1,否則0 numeric: 若欄位為數值則為1,否則0 blob: 若欄位為BLOB則為1,否則為0 type: 欄位的資料類型 unsigned: 若欄位為無符號數則為1,否則為0 zerofill: 若欄位為「零填充」則為1, 否則為0 取得查詢的欄位數
mysql_num_fields()
複製程式碼
(string database_name, string table_name [, resource link_id])$fields = mysql_list_fields("MyDatabase", "MyTable");echo "資料庫MyDatabaseMyTableTableTable集的欄位數: ".mysql_num_fields($fields) ." 複製程式碼
mysql_field_flags()string mysql_field_flags (resource result_set, integerfield_offset)
integer field_offset)$query = "select name from MyTable";$result = mysql_query($query);$row = mysql_fetch_row($result);echocho mysqlfield_lencho(fetch_row($result); echo mysql_ult_lencho mysql,0$ ).""; 如果mysql_field_len($reseult, 0) = 16777215
那麼numer_format(mysql_field_len($result))等於16,777,215
mysql_field_name()mysql_field_name()mysql_field_name() $query = "select id as PKID, name from MyTable order by name"; 取得欄位類型
mysql_field_type()string mysql_field_type (resource result_set, int field_offset) Myquery (resource result_set, int field_offset) by name";$result = mysql_query($query); $row = mysql_fetch_row($result); 取得欄位所在表名
|