码迷,mamicode.com
首页 > 数据库 > 详细

php mySql常用的函数

时间:2020-02-16 20:32:18      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:col   local   表格   php   class   data   unsigned   mysq   对象属性   

  1 1、mysql_connect()-建立数据库连接
  2 格式:
  3 resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
  4 例:
  5 $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
  6 说明:使用该连接必须显示的关闭连接
  7 
  8 2、mysql_pconnect()-建立数据库连接
  9 格式:
 10 resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
 11 例:
 12 $conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");
 13 说明:使用该连接函数不需要显示的关闭连接,它相当于使用了连接池
 14 
 15 3、mysql_close()-关闭数据库连接
 16 例:
 17 $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
 18 @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
 19 echo "你已经连接到MyDatabase数据库";
 20 mysql_close();
 21 
 22 4、mysql_select_db()-选择数据库
 23 格式:
 24 boolean mysql_select_db(string db_name [, resource link_id])
 25 例:
 26 $conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
 27 @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
 28 
 29 5、mysql_query()-查询MySQL
 30 格式:
 31 resource mysql_query (string query, [resource link_id])
 32 例:
 33 $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
 34 @mysql_select_db("MyDatabase") or die("不能选择这个数据库,或者数据库不存在");
 35 $query = "select * from MyTable";
 36 $result = mysql_query($query);
 37 mysql_close();
 38 说明:若SQL查询执行成功,则返回资源标识符,失败时返回FALSE。若执行更新成功,则返回TRUE,否则返回FALSE
 39 
 40 6、mysql_db_query()-查询MySQL
 41 格式:
 42 resource mysql_db_query(string database, string query [, resource link_id])
 43 例:
 44 $linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到MysqlServer");
 45 $query = "select * from MyTable";
 46 $result = mysql_db_query("MyDatabase", $query);
 47 mysql_close();
 48 说明:为了使代码清晰,不推荐使用这个函数调用
 49 
 50 7、mysql_result()-获取和显示数据
 51 格式:
 52 mixed mysql_result (resource result_set, int row [, mixed field])
 53 例:
 54 $query = "select id, name from MyTable order by name";
 55 $result = mysql_query($query);
 56 for($count=0;$count<=mysql_numrows($result);$count++)
 57 {
 58 $c_id = mysql_result($result, 0, "id");
 59 $c_name = mysql_result($result, 0, "name");
 60 echo $c_id,$c_name;
 61 }
 62 说明:最简单、也是效率最低的数据获取函数
 63 
 64 8、mysql_fetch_row()-获取和显示数据
 65 格式:
 66 array mysql_fetch_row (resource result_set)
 67 例:
 68 $query = "select id, name from MyTable order by name";
 69 $result = mysql_query($query);
 70 while (list($id, $name) = mysql_fetch_row($result)) {
 71 echo("Name: $name ($id) <br />");
 72 }
 73 说明:函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用
 74 
 75 9、mysql_fetch_array()-获取和显示数据
 76 格式:
 77 array mysql_fetch_array (resource result_set [, int result_type])
 78 例:
 79 $query = "select id, name from MyTable order by name";
 80 $result = mysql_query($query);
 81 while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 82 $id = $row["id"];
 83 $name = $row["name"];
 84 echo "Name: $name ($id) <br />";
 85 }
 86 又例:
 87 $query = "select id, name from MyTable order by name";
 88 $result = mysql_query($query);
 89 while($row = mysql_fetch_array($result, MYSQL_NUM)) {
 90 $id = $row[0];
 91 $name = $row[1];
 92 echo "Name: $name ($id) <br />";
 93 }
 94 说明:
 95 result_type的值有:
 96 MYSQL_ASSOC: 字段名表示键,字段内容为值
 97 MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样
 98 MYSQL_BOTH: 即作为关联数组又作为数值索引数组返回。result_type的默认值。
 99 
100 10、mysql_fetch_assoc()-获取和显示数据
101 格式:
102 array mysql_fetch_assoc (resource result_set)
103 相当于调用 mysql_fetch_array(resource, MYSQL_ASSOC);
104 
105 11、mysql_fetch_object()-获取和显示数据
106 格式:
107 object mysql_fetch_object(resource result_set)
108 例:
109 $query = "select id, name from MyTable order by name";
110 while ($row = mysql_fetch_object($result)) {
111 $id = $row->id;
112 $name = $row->name;
113 echo "Name: $name ($id) <br />";
114 }
115 说明:返回一个对象,在操作上与mysql_fetch_array()相同
116 
117 12、mysql_num_rows()-所选择的记录的个数
118 格式:
119 int mysql_num_rows(resource result_set)
120 例:
121 query = "select id, name from MyTable where id > 65";
122 $result = mysql_query($query);
123 echo "有".mysql_num_rows($result)."条记录的ID大于65";
124 说明:只在确定select查询所获取的记录数时才有用。
125 
126 13、mysql_affected_rows()-受Insert,update,delete影响的记录的个数
127 格式:
128 int mysql_affected_rows([resource link_id])
129 例:
130 $query = "update MyTable set name=‘CheneyFu‘ where id>=5";
131 $result = mysql_query($query);
132 echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();
133 说明:该函数获取受INSERT,UPDATE或DELETE更新语句影响的行数
134 
135 14、mysql_list_dbs()-获取数据库列表信息
136 格式:
137 resource mysql_list_dbs([resource link_id])
138 例:
139 mysql_connect("localhost", "username", "password");
140 $dbs = mysql_list_dbs();
141 echo "Databases: <br />";
142 while (list($db) = mysql_fetch_rows($dbs)) {
143 echo "$db <br />";
144 }
145 说明:显示所有数据库名称
146 
147 15、mysql_db_name()-获取数据库名
148 格式:
149 string mysql_db_name(resource result_set, integer index)
150 说明:该函数获取在mysql_list_dbs()所返回result_set中位于指定index索引的数据库名
151 
152 16、mysql_list_tables()-获取数据库表列表
153 格式:
154 resource mysql_list_tables(string database [, resource link_id])
155 例:
156 mysql_connect("localhost", "username", "password");
157 $tables = mysql_list_tables("MyDatabase");
158 while (list($table) = mysql_fetch_row($tables)) {
159 echo "$table <br />";
160 }
161 说明:该函数获取database中所有表的表名
162 
163 17、mysql_tablename()-获取某个数据库表名
164 格式:
165 string mysql_tablename(resource result_set, integer index)
166 例:
167 mysql_connect("localhost", "username", "password");
168 $tables = mysql_list_tables("MyDatabase");
169 $count = -1;
170 while (++$count < mysql_numrows($tables)) {
171 echo mysql_tablename($tables, $count)."<br />";
172 }
173 说明:该函数获取mysql_list_tables()所返回result_set中位于指定index索引的表名
174 
175 18、mysql_fetch_field()-获取字段信息
176 格式:
177 object mysql_fetch_field(resource result [, int field_offset])
178 例:
179 mysql_connect("localhost", "username", "password");
180 mysql_select_db("MyDatabase");
181 $query = "select * from MyTable";
182 $result = mysql_query($query);
183 $counts = mysql_num_fields($result);
184 for($count = 0; $count < $counts; $count++) {
185 $field = mysql_fetch_field($result, $count);
186 echo "<p>$field->name $field->type ($field->max_length) </p>";
187 }
188 说明:
189 返回的对象共有12个对象属性:
190 name: 字段名
191 table: 字段所在的表
192 max_length:字段的最大长度
193 not_null: 如果字段不能为null,则为1,否则0
194 primary_key: 如果字段为主键,则为1,否则0
195 unique_key: 如果字段是唯一键,则为1, 否则0
196 multiple_key: 如果字段为非唯一,则为1,否则0
197 numeric: 如果字段为数值则为1,否则0
198 blob: 如果字段为BLOB则为1,否则为0
199 type: 字段的数据类型
200 unsigned: 如果字段为无符号数则为1,否则为0
201 zerofill: 如果字段为“零填充”则为1, 否则为0
202 
203 19、mysql_num_fields()-获取查询的字段个数
204 格式:
205 integer mysql_num_fields(resource result_set)
206 例:
207 $query = "select id,name from MyTable order by name";
208 $result = mysql_query($query);
209 echo "这个查询的字段数是:".mysql_num_fields($result)."<br />";
210 
211 20、mysql_list_fields()-获取指定表的所有字段的字段名
212 格式:
213 resource mysql_list_fields (string database_name, string table_name [, resource link_id])
214 例:
215 $fields =mysql_list_fields("MyDatabase", "MyTable");
216 echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."<br />";
217 
218 21、mysql_field_flags()-获取指定的字段选项
219 格式:
220 string mysql_field_flags (resource result_set, integer field_offset)
221 例:
222 $query = "select id, name from MyTable order by name";
223 $result = mysql_query($query);
224 $row=mysql_fetch_wor($row);
225 
226 22、mysql_field_len()-获取指定的字段的最大长度
227 格式:
228 integer mysql_field_len (resource result_set, integer field_offset)
229 例:
230 $query = "select name from MyTable";
231 $result = mysql_query($query);
232 $row = mysql_fetch_row($result);
233 echo mysql_field_len($result, 0)."<br />";
234 说明:
235 如果mysql_field_len($reseult, 0) = 16777215
236 那么numer_format(mysql_field_len($result))等于16,777,215
237 
238 23、mysql_field_name()-获取字段名
239 格式:
240 string mysql_field_name (resource result_set, int field_offset)
241 例:
242 $query = "select id as PKID, name from MyTable order by name";
243 $result = mysql_query($query);
244 $row = mysql_fetch_row($result);
245 echo mysql_field_name($result, 0); // Result: PKID
246 
247 24、mysql_field_type()-获取字段类型
248 格式:
249 string mysql_field_type (resource result_set, int field_offset)
250 例:
251 $query = "select id, name from MyTable order by name";
252 $result = mysql_query($query);
253 $row = mysql_fetch_row($result);
254 echo mysql_field_type($result, 0); // Result: int
255 
256 25、mysql_field_table()-获取字段所在表名
257 格式:
258 string mysql_field_table (resource result_set, int field_offset)
259 例:
260 $query = "select id as PKID, name from MyTable order by name";
261 $result = mysql_query($query);
262 $row = mysql_fetch_row($result);
263 echo mysql_field_table($result, 0); // Result: MyTable 

 

1、mysql_connect()-建立数据库连接格式:resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])例:$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]])例:$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");说明:使用该连接函数不需要显示的关闭连接,它相当于使用了连接池
3、mysql_close()-关闭数据库连接例:$conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");echo "你已经连接到MyDatabase数据库";mysql_close();
4、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("不能选择这个数据库,或数据库不存在");
5、mysql_query()-查询MySQL格式:resource mysql_query (string query, [resource link_id])例:$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
6、mysql_db_query()-查询MySQL格式: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();说明:为了使代码清晰,不推荐使用这个函数调用
7、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);for($count=0;$count<=mysql_numrows($result);$count++){$c_id = mysql_result($result, 0, "id");$c_name = mysql_result($result, 0, "name");echo $c_id,$c_name;}说明:最简单、也是效率最低的数据获取函数
8、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 />");}说明:函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用
9、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 />";}又例:$query = "select id, name from MyTable order by name";$result = mysql_query($query);while($row = mysql_fetch_array($result, MYSQL_NUM)) {$id = $row[0];$name = $row[1];echo "Name: $name ($id) <br />";}说明: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)例:$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()相同
12、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查询所获取的记录数时才有用。
13、mysql_affected_rows()-受Insert,update,delete影响的记录的个数格式:int mysql_affected_rows([resource link_id])例:$query = "update MyTable set name=‘CheneyFu‘ where id>=5";$result = mysql_query($query);echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();说明:该函数获取受INSERT,UPDATE或DELETE更新语句影响的行数
14、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 />";}说明:显示所有数据库名称
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])例:mysql_connect("localhost", "username", "password");$tables = mysql_list_tables("MyDatabase");while (list($table) = mysql_fetch_row($tables)) {echo "$table <br />";}说明:该函数获取database中所有表的表名
17、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 />";}说明:该函数获取mysql_list_tables()所返回result_set中位于指定index索引的表名
18、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);$counts = mysql_num_fields($result);for($count = 0; $count < $counts; $count++) {$field = mysql_fetch_field($result, $count);echo "<p>$field->name $field->type ($field->max_length) </p>";}说明:返回的对象共有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)例:$query = "select id,name from MyTable order by name";$result = mysql_query($query);echo "这个查询的字段数是:".mysql_num_fields($result)."<br />";
20、mysql_list_fields()-获取指定表的所有字段的字段名格式:resource mysql_list_fields (string database_name, string table_name [, resource link_id])例:$fields =mysql_list_fields("MyDatabase", "MyTable");echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."<br />";
21、mysql_field_flags()-获取指定的字段选项格式:string mysql_field_flags (resource result_set, integer field_offset)例:$query = "select id, name from MyTable order by name";$result = mysql_query($query);$row=mysql_fetch_wor($row);
22、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
23、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
24、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
25、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 

php mySql常用的函数

标签:col   local   表格   php   class   data   unsigned   mysq   对象属性   

原文地址:https://www.cnblogs.com/weihexinCode/p/12318382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!