php資料庫作業碼大全

來源:互聯網
上載者:User
  1. $conn = @mysql_connect("localhost", "username", "password") or die("不能串連到Mysql Server");
複製代碼

說明:使用該串連必須顯示的關閉串連

2、mysql_pconnect()-建立資料庫連接格式:resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])例:

  1. $conn = @mysql_pconnect("localhost", "username", "password") or dir("不能串連到Mysql Server");
複製代碼

說明:使用該串連函數不需要顯示的關閉串連,它相當於使用了串連池

3、mysql_close()-關閉資料庫連接例:

  1. $conn = @mysql_connect("localhost", "username", "password") or die("不能串連到Mysql Server");
  2. @mysql_select_db("MyDatabase") or die("不能選擇這個資料庫,或資料庫不存在");
  3. echo "你已經串連到MyDatabase資料庫";
  4. mysql_close();
複製代碼

4、mysql_select_db()-選擇資料庫格式:boolean mysql_select_db(string db_name [, resource link_id])例:

  1. $conn = @mysql_connect("localhost", "username", "password") or die("不能串連到Mysql Server");
  2. @mysql_select_db("MyDatabase") or die("不能選擇這個資料庫,或資料庫不存在");
複製代碼

5、mysql_query()-查詢MySQL格式:resource mysql_query (string query, [resource link_id])例:

  1. $linkId = @mysql_connect("localhost", "username", "password") or die("不能串連到Mysql Server");
  2. @mysql_select_db("MyDatabase") or die("不能選擇這個資料庫,或者資料庫不存在");
  3. $query = "select * from MyTable";
  4. $result = mysql_query($query);
  5. mysql_close();
複製代碼

說明:若SQL查詢執行成功,則返回資源標識符,失敗時返回FALSE。若執行更新成功,則返回TRUE,否則返回FALSE

6、mysql_db_query()-查詢MySQL格式:resource mysql_db_query(string database, string query [, resource link_id])例:

  1. $linkId = @mysql_connect("localhost", "username", "password") or die("不能串連到MysqlServer");
  2. $query = "select * from MyTable";
  3. $result = mysql_db_query("MyDatabase", $query);
  4. mysql_close();
複製代碼

說明:為了使代碼清晰,不推薦使用這個函數調用

7、mysql_result()-擷取和顯示資料格式:mixed mysql_result (resource result_set, int row [, mixed field])例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. for($count=0;$count<=mysql_numrows($result);$count++)
  4. {
  5. $c_id = mysql_result($result, 0, "id");
  6. $c_name = mysql_result($result, 0, "name");
  7. echo $c_id,$c_name;
  8. }
複製代碼

說明:最簡單、也是效率最低的資料擷取函數

8、mysql_fetch_row()-擷取和顯示資料格式:array mysql_fetch_row (resource result_set)例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while (list($id, $name) = mysql_fetch_row($result)) {
  4. echo("Name: $name ($id)
    ");
  5. }
複製代碼

說明:函數從result_set中擷取整個資料行,將值放在一個索引數組中。通常會結使list()函數使用

9、mysql_fetch_array()-擷取和顯示資料格式:array mysql_fetch_array (resource result_set [, int result_type])例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  4. $id = $row["id"];
  5. $name = $row["name"];
  6. echo "Name: $name ($id)
    ";
  7. }
複製代碼

又例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. while($row = mysql_fetch_array($result, MYSQL_NUM)) {
  4. $id = $row[0];
  5. $name = $row[1];
  6. echo "Name: $name ($id)
    ";
  7. }
複製代碼

說明:result_type的值有:MYSQL_ASSOC: 欄位名表示鍵,欄位內容為值MYSQL_NUM: 數值索引數組,操作與mysql_fetch_ros()函數一樣MYSQL_BOTH: 即作為關聯陣列又作為數值索引數組返回。result_type的預設值。

10、mysql_fetch_assoc()-擷取和顯示資料格式:array mysql_fetch_assoc (resource result_set)相當於調用 mysql_fetch_array(resource, MYSQL_ASSOC);

11、mysql_fetch_object()-擷取和顯示資料格式:object mysql_fetch_object(resource result_set)例:

  1. $query = "select id, name from MyTable order by name";
  2. while ($row = mysql_fetch_object($result)) {
  3. $id = $row->id;
  4. $name = $row->name;
  5. echo "Name: $name ($id)
    ";
  6. }
複製代碼

說明:返回一個對象,在操作上與mysql_fetch_array()相同

12、mysql_num_rows()-所選擇的記錄的個數格式:int mysql_num_rows(resource result_set)例:

  1. query = "select id, name from MyTable where id > 65";
  2. $result = mysql_query($query);
  3. echo "有".mysql_num_rows($result)."條記錄的ID大於65";
複製代碼

說明:只在確定select查詢所擷取的記錄數時才有用。

13、mysql_affected_rows()-受Insert,update,delete影響的記錄的個數格式:int mysql_affected_rows([resource link_id])例:

  1. $query = "update MyTable set name='CheneyFu' where id>=5";
  2. $result = mysql_query($query);
  3. echo "ID大於等於5的名稱被更新了的記錄數:".mysql_affected_rows();
複製代碼

說明:該函數擷取受INSERT,UPDATE或DELETE更新語句影響的行數

14、mysql_list_dbs()-擷取資料庫列表資訊格式:resource mysql_list_dbs([resource link_id])例:

  1. mysql_connect("localhost", "username", "password");
  2. $dbs = mysql_list_dbs();
  3. echo "Databases:
    ";
  4. while (list($db) = mysql_fetch_rows($dbs)) {
  5. echo "$db
    ";
  6. }
複製代碼

說明:顯示所有資料庫名稱

15、mysql_db_name()-擷取資料庫名格式:string mysql_db_name(resource result_set, integer index)說明:該函數擷取在mysql_list_dbs()所返回result_set中位於指定index索引的資料庫名

16、mysql_list_tables()-擷取資料庫表列表格式:resource mysql_list_tables(string database [, resource link_id])例:

  1. mysql_connect("localhost", "username", "password");
  2. $tables = mysql_list_tables("MyDatabase");
  3. while (list($table) = mysql_fetch_row($tables)) {
  4. echo "$table
    ";
  5. }
複製代碼

說明:該函數擷取database中所有表的表名

17、mysql_tablename()-擷取某個資料庫表名格式:string mysql_tablename(resource result_set, integer index)例:

  1. mysql_connect("localhost", "username", "password");
  2. $tables = mysql_list_tables("MyDatabase");
  3. $count = -1;
  4. while (++$count < mysql_numrows($tables)) {
  5. echo mysql_tablename($tables, $count)."
    ";
  6. }
複製代碼

說明:該函數擷取mysql_list_tables()所返回result_set中位於指定index索引的表名

18、mysql_fetch_field()-擷取欄位資訊格式:object mysql_fetch_field(resource result [, int field_offset])例:

  1. mysql_connect("localhost", "username", "password");
  2. mysql_select_db("MyDatabase");
  3. $query = "select * from MyTable";
  4. $result = mysql_query($query);
  5. $counts = mysql_num_fields($result);
  6. for($count = 0; $count < $counts; $count++) {
  7. $field = mysql_fetch_field($result, $count);
  8. echo "

    $field->name $field->type ($field->max_length)

    ";
  9. }
複製代碼

說明:返回的對象共有12個對象屬性:name: 欄位名table: 欄位所在的表max_length:欄位的最大長度not_null: 如果欄位不能為null,則為1,否則0primary_key: 如果欄位為主鍵,則為1,否則0unique_key: 如果欄位是唯一鍵,則為1, 否則0multiple_key: 如果欄位為非唯一,則為1,否則0numeric: 如果欄位為數值則為1,否則0blob: 如果欄位為BLOB則為1,否則為0type: 欄位的資料類型unsigned: 如果欄位為無符號數則為1,否則為0zerofill: 如果欄位為“零填充”則為1, 否則為0

19、mysql_num_fields()-擷取查詢的欄位個數格式:integer mysql_num_fields(resource result_set)例:

  1. $query = "select id,name from MyTable order by name";
  2. $result = mysql_query($query);
  3. echo "這個查詢的欄位數是:".mysql_num_fields($result)."
    ";
複製代碼

20、mysql_list_fields()-擷取指定表的所有欄位的欄位名格式:resource mysql_list_fields (string database_name, string table_name [, resource link_id])例:

  1. $fields =mysql_list_fields("MyDatabase", "MyTable");
  2. echo "資料庫MyDatabase中表MyTable的欄位數: ".mysql_num_fields($fields)."
    ";
複製代碼

21、mysql_field_flags()-擷取指定的欄位選項格式:string mysql_field_flags (resource result_set, integer field_offset)例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row=mysql_fetch_wor($row);
複製代碼

22、mysql_field_len()-擷取指定的欄位的最大長度格式:integer mysql_field_len (resource result_set, integer field_offset)例:

  1. $query = "select name from MyTable";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_len($result, 0)."
    ";
複製代碼

說明:如果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)例:

  1. $query = "select id as PKID, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_name($result, 0); // Result: PKID
複製代碼

24、mysql_field_type()-擷取欄位類型格式:string mysql_field_type (resource result_set, int field_offset)例:

  1. $query = "select id, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_type($result, 0); // Result: int
複製代碼

25、mysql_field_table()-擷取欄位所在表名格式:string mysql_field_table (resource result_set, int field_offset)例:

  1. $query = "select id as PKID, name from MyTable order by name";
  2. $result = mysql_query($query);
  3. $row = mysql_fetch_row($result);
  4. echo mysql_field_table($result, 0); // Result: MyTable
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.