色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

PHP與MYSQL交互函數(shù)表學(xué)習(xí)筆記

瀏覽:6日期:2024-02-12 13:59:29
最近一直在研究PHP與MYSQL,感覺(jué)PHP與MYSQL交互的函數(shù)都是過(guò)程化的,當(dāng)然也有mysqli擴(kuò)展,面向?qū)ο?,Java和C#寫(xiě)多了之后,再寫(xiě)PHP,有些不適應(yīng),感覺(jué)又回到了學(xué)C的年代。今天學(xué)習(xí)了一些函數(shù),記錄下來(lái),以便日后忘記時(shí),可以參考。

說(shuō) 明函 數(shù) 名函 數(shù) 詳 細(xì)函 數(shù) 說(shuō) 明建立數(shù)據(jù)庫(kù)連接mysql_connect()resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])示例:$conn = @mysql_connect('localhost', 'username', 'password') or dir('不能連接到Mysql Server');使用該連接必須顯示的關(guān)閉連接建立數(shù)據(jù)庫(kù)連接mysql_pconnect()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)閉連接,它相當(dāng)于使用了連接池關(guān)閉數(shù)據(jù)庫(kù)連接mysql_close()$conn = @mysql_connect('localhost', 'username', 'password') or die('不能連接到Mysql Server');@mysql_select_db('MyDatabase') or die('不能選擇這個(gè)數(shù)據(jù)庫(kù),或數(shù)據(jù)庫(kù)不存在');echo '你已經(jīng)連接到MyDatabase數(shù)據(jù)庫(kù)';mysql_close() 選擇數(shù)據(jù)庫(kù)mysql_select_db()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('不能選擇這個(gè)數(shù)據(jù)庫(kù),或數(shù)據(jù)庫(kù)不存在'); 查詢MySQLmysql_query()resource mysql_query (string query, [resource link_id])$linkId = @mysql_connect('localhost', 'username', 'password') or die('不能連接到Mysql Server');@mysql_select_db('MyDatabase') or die('不能選擇這個(gè)數(shù)據(jù)庫(kù),或者數(shù)據(jù)庫(kù)不存在');$query = 'select * from MyTable';$result = mysql_query($query);mysql_close();若SQL查詢執(zhí)行成功,則返回資源標(biāo)識(shí)符,失敗時(shí)返回FALSE。若執(zhí)行更新成功,則返回TRUE,否則返回FALSE查詢MySQLmysql_db_query()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();為了使代碼清晰,不推薦使用這個(gè)函數(shù)調(diào)用獲取和顯示數(shù)據(jù)mysql_result()mixed mysql_result (resource result_set, int row [, mixed field])$query = 'select id, name from MyTable order by name';$result = mysql_query($query);$c_id = mysql_result($result, 0, 'id');$c_name = mysql_result($result, 0, 'name');最簡(jiǎn)單、也是效率最低的數(shù)據(jù)獲取函數(shù)獲取和顯示數(shù)據(jù)mysql_fetch_row()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中獲取整個(gè)數(shù)據(jù)行,將值放在一個(gè)索引數(shù)組中。通常會(huì)結(jié)使list()函數(shù)使用獲取和顯示數(shù)據(jù)mysql_fetch_array()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 />';}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的默認(rèn)值。獲取和顯示數(shù)據(jù)mysql_fetch_assoc()array mysql_fetch_assoc (resource result_set)相當(dāng)于調(diào)用 mysql_fetch_array(resource, MYSQL_ASSOC); 獲取和顯示數(shù)據(jù)mysql_fetch_object()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()相同所選擇的記錄mysql_num_rows()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ù)時(shí)才有用。受影響的記錄mysql_affected_rows()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更新語(yǔ)句影響的行數(shù)獲取數(shù)據(jù)庫(kù)列表信息mysql_list_dbs()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ù)庫(kù)名mysql_db_name()string mysql_db_name(resource result_set, integer index)該函數(shù)獲取在mysql_list_dbs()所返回result_set中位于指定index索引的數(shù)據(jù)庫(kù)名獲取數(shù)據(jù)庫(kù)表列表mysql_list_tables()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中所有表的表名獲取數(shù)據(jù)庫(kù)表名mysql_tablename()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索引的表名獲取字段信息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);$fields = mysql_num_fields($result);for($count = 0; $count < $fieds; $count++) { $field = mysql_fetch_field($result, $count); echo '<p>$field->name $field->type ($field->max_length) </p>';}返回的對(duì)象共有12個(gè)對(duì)象屬性:name: 字段名table: 字段所在的表max_length: 字段的最大長(zhǎng)度not_null: 如果字段不能為null,則為1,否則0primary_key: 如果字段為主鍵,則為1,否則0unique_key: 如果字段是唯一鍵,則為1, 否則0multiple_key: 如果字段為非唯一,則為1,否則0numeric: 如果字段為數(shù)值則為1,否則0blob: 如果字段為BLOB則為1,否則為0type: 字段的數(shù)據(jù)類型unsigned: 如果字段為無(wú)符號(hào)數(shù)則為1,否則為0zerofill: 如果字段為“零填充”則為1, 否則為0獲取查詢的字段數(shù)mysql_num_fields()integer mysql_num_fields (resource result_set)$query = 'select id, name from MyTable order by name';$result = mysql_query($query);echo '這個(gè)查詢的字段數(shù)是:'.mysql_num_fields($result).'<br />';返回查詢r(jià)esult_set中的字段數(shù)獲取指定表的所有字段的字段名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ù)庫(kù)MyDatabase中表MyTable的字段數(shù): '.mysql_num_fields($fields).'<br />'; 獲取指定的字段選項(xiàng)mysql_field_flags()string mysql_field_flags (resource result_set, integer field_offset) 獲取指定的字段的最大長(zhǎng)度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獲取字段名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 獲取字段類型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 獲取字段所在表名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

標(biāo)簽: PHP
主站蜘蛛池模板: 成人精品| 国产黄色自拍视频 | 欧美日本俄罗斯一级毛片 | 精品国产中文一级毛片在线看 | 国产精品合集一区二区 | 国产精品三 | 久久久一本 | 黄色三级视频在线 | 欧美日韩高清在线观看一区二区 | 国产成人精品综合久久久软件 | 国产做国产爱免费视频 | 成人 在线播放 | 在线观看免费黄视频 | 亚洲精品一区二区三区五区 | 国产一区二区三区不卡在线观看 | 国产爱啪啪 | 久久久黄色大片 | 亚洲黄色在线视频 | 欧美 自拍 丝袜 亚洲 | 亚洲免费视频观看 | 加勒比综合网 | 日本一级毛片片在线播放 | a级片在线免费看 | 一级美女片 | 日韩国产免费一区二区三区 | 亚洲免费在线视频 | 国产精品亚洲一区二区三区在线观看 | 亚洲精品一级片 | 九九精品视频在线观看九九 | 一级女性全黄生活片免费 | 色偷偷成人 | 国产精品一区二区三区免费 | 欧美日韩国产成人精品 | 久久亚洲精品中文字幕二区 | 精品国产一区二区三区久 | 亚洲国产精品一区二区三区 | 生活片一级播放免费 | 精品国产免费一区二区三区五区 | 在线久草视频 | 免费国产黄网站在线观看视频 | 精品国产高清久久久久久小说 |