Home Backend Development PHP Tutorial PHP中的MYSQL常用函数(php下操作数据库必备)_PHP

PHP中的MYSQL常用函数(php下操作数据库必备)_PHP

Jun 01, 2016 pm 12:17 PM
php database

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{
$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)
");
}
说明:函数从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)
";
}
又例:
$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)
";
}
说明:
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)
";
}
说明:返回一个对象,在操作上与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:
";
while (list($db) = mysql_fetch_rows($dbs)) {
echo "$db
";
}
说明:显示所有数据库名称

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
";
}
说明:该函数获取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 echo mysql_tablename($tables, $count)."
";
}
说明:该函数获取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 $field = mysql_fetch_field($result, $count);
echo "

$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

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)."
";

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)."
";

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)."
";
说明:
如果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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles