PHP串連MYSQL的兩種方法

來源:互聯網
上載者:User
  mysql可通過兩種方式通過PHP與web相連,一種通過php的mysql相關函數,另一種通過php的ODBC相關函數

相關函數如下:

MYSQL函數

mysql_affected_rows: 得到 MySQL 最後操作影響的列數目。
mysql_close: 關閉 MySQL 伺服器連線。
mysql_connect: 開啟 MySQL 伺服器連線。
mysql_create_db: 建立一個 MySQL 新資料庫。
mysql_data_seek: 移動內部傳回指標。
mysql_db_query: 送查詢字串 (query) 到 MySQL 資料庫。
mysql_drop_db: 移除資料庫。
mysql_errno: 傳回錯誤訊息代碼。
mysql_error: 傳回錯誤訊息。
mysql_fetch_array: 傳回陣列資料。
mysql_fetch_field: 取得欄位資訊。
mysql_fetch_lengths: 傳回單列各欄資料最大長度。
mysql_fetch_object: 傳回物件資料。
mysql_fetch_row: 傳回單列的各欄位。
mysql_field_name: 傳回指定欄位的名稱。
mysql_field_seek: 設定指標到傳回值的某欄位。
mysql_field_table: 獲得目前欄位的資料表 (table) 名稱。
mysql_field_type: 獲得目前欄位的型態。
mysql_field_flags: 獲得目前欄位的旗標。
mysql_field_len: 獲得目前欄位的長度。
mysql_free_result: 釋放傳回佔用記憶體。
mysql_insert_id: 傳回最後一次使用 INSERT 指令的 ID。
mysql_list_fields: 列出指定資料表的欄位 (field)。
mysql_list_dbs: 列出 MySQL 伺服器可用的資料庫 (database)。
mysql_list_tables: 列出指定資料庫的資料表 (table)。
mysql_num_fields: 取得傳回欄位的數目。
mysql_num_rows: 取得傳回列的數目。
mysql_pconnect: 開啟 MySQL 伺服器長期連線。
mysql_query: 送出一個 query 字串。
mysql_result: 取得查詢 (query) 的結果。
mysql_select_db: 選擇一個資料庫。
mysql_tablename: 取得資料表名稱。
ODBC函數

使用ODBC函數需安裝MYSQL ODBC

odbc_autocommit: 開關自動更動功能。
odbc_binmode: 設定二進位資料處理方式。
odbc_close: 關閉 ODBC 連結。
odbc_close_all: 關閉所有 ODBC 連結。
odbc_commit: 更動 ODBC 資料庫。
odbc_connect: 連結至 ODBC 資料庫。
odbc_cursor: 取得遊標名。
odbc_do: 執行 SQL 指令。
odbc_exec: 執行 SQL 指令。
odbc_execute: 執行預置 SQL 指令。
odbc_fetch_into: 取得傳回的指定列。
odbc_fetch_row: 取得傳回一列。
odbc_field_name: 取得欄位名稱。
odbc_field_type: 取得欄位資料形態。
odbc_field_len: 取得欄位資料長度。
odbc_free_result: 釋出傳回資料的記憶體。
odbc_longreadlen: 設定傳回欄的最大值。
odbc_num_fields: 取得欄位元目。
odbc_pconnect: 長期連結至 ODBC 資料庫。
odbc_prepare: 預置 SQL 指令。
odbc_num_rows: 取得傳回列數目。
odbc_result: 取得傳回資料。
odbc_result_all: 傳回 HTML 表格資料。
odbc_rollback: 撤消當前交易。
odbc_setoption: 調整 ODBC 設定。

例子可見我在本網站的"程式與代碼"裡的文章和下面的常式:

前台HTML

< form enctype="multipart/form-data" action="hb_ggzd.php" method="post" >
< tr >
< td width="251" >< small >您 可 以 花 些 時 間 填 寫 下 表 , 也 可 以 用 電
子 郵 件 聯 系 ,< a href="mailto:likai333@21cn.com" >likai333@21cn.com< /a >。< /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="31" >< small >您的姓名:< /small >< /td >
< td width="462" height="31" >< small >< input name="name" size="20" > < /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="30" >< small >您的電子郵件:< /small >< /td >
< td width="462" height="30" >< small >< input name="usermail" size="37" >< /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="25" >< small >您欲做廣告的網站地址:< /small >< /td >
< td width="462" height="25" >< small >< small >< input name="userurl" size="37" value="http://" >< /small >< /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="25" >< small >您欲做廣告的網站站名:< /small >< /td >
< td width="462" height="25" >< small >< input name="sitename" size="20" >< /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="30" >< small >您的公司名稱:< /small >< /td >
< td width="462" height="30" >< small >< input name="company" size="37" >< /small >< /td >
< /tr >
< tr >
< td vAlign="top" width="251" height="25" >< small >您的聯絡電話:< /small >< /td >
< td width="462" height="25" >< small >< input name="phone" size="20" >< /small >< /td >
< /tr >
< tr >

後台PHP

< ?php
= mysql_connect('localhost:3306','root','');
mysql_select_db('HBWEB');
="insert into usemsg ( name,usermail,userurl,sitename, company,phone) values ('','','', '', '','','','','','','')";
= mysql_query(, );
= mysql_error();
if(){
echo "發生錯誤,請通知< a href=mailto:likai333@21cn.com >我< /a >";
echo "";
}
else
{ echo "成功入庫"; }

? >

表結構:

CREATE TABLE usemsg (
name varchar(255) not null default "notname",
usermail varchar(255) not null default "notusermail",
userurl varchar(255) not null default "notuserurl",
sitename varchar(255) not null default "notsitename",
company varchar(255) not null default "notcompany",
phone varchar(255) not null default "notphone",
);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.