php mysqlmysqliPDO (1) mysql, mysqlipdo_PHP tutorial
php mysqlmysqliPDO (1) mysql, mysqlipdo
Original link: http://www.orlion.ga/1140/
The operations of the database at work are all encapsulated. I almost forgot how to use them, so I just wrote a blog to review them. If I forget again in the future, you can read this article.
Deprecated since PHP 5.5.0
1. mysql_connect()
resource mysql_connect([ string $server [, string $username [, string $password [, bool$new_link [, int $client_flags ]]]]] )
$server: The server address can include the port number. If the PHP directive mysql.default_host is not defined (the default), the default value is 'localhost:3306'. In SQL safe mode, the parameter is ignored and 'localhost:3306' is always used.
$username: username. The default value is defined by mysql.default_user. In SQL safe mode, the parameter is ignored and the username of the server process owner is always used.
$password: Password. The default value is defined by mysql.default_password. In SQL safe mode, the parameter is ignored and an empty password is always used.
$new_link: If mysql_connect() is called a second time with the same parameters, a new connection will not be established, but the already opened connection ID will be returned. Parameter new_link
changes this behavior and makes mysql_connect() always open a new connection, even when mysql_connect() has been called previously with the same parameters.
$client_flags: Use the following constants:
Constant | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MYSQL_CLIENT_COMPRESS |
Use compressed communication protocol | ||||||||||
MYSQL_CLIENT_IGNORE_SPACE |
Allow spaces after function names | ||||||||||
MYSQL_CLIENT_INTERACTIVE |
Allows setting the interactive_timeout time to wait idle before disconnecting (instead of wait_timeout). | ||||||||||
MYSQL_CLIENT_SSL |
Use SSL encryption. This flag is only available when MySQL client library version is 4.x or higher. Both PHP 4 and Windows versions of PHP 5 are bundled with 3.23.x.
|
2、mysql_close()
bool mysql_close ([ resource $link_identifier = NULL ] )
mysql_close() 关闭指定的连接标识所关联的到 MySQL 服务器的非持久连接。如果没有指定 link_identifier
,则关闭上一个打开的连接.通常不需要使用 mysql_close(),因为已打开的非持久连接会在脚本执行完毕后自动关闭。PHP 4 Zend 引擎引进了引用计数系统,可以自动检测到一个资源不再被引用了(和 Java 一样)。这种情况下此资源使用的所有外部资源都会被垃圾回收系统释放。因此,很少需要手工释放内存。
3、mysql_select_db()
bool mysql_select_db ( string $database_name [, resource $ link_identifier ] )
如果没有指定$link_identifier则使用上一个打开的数据库连接,如果没有打开的连接则将无参数调用mysql_connet()取得一个连接并使用。如果没有连接则E_WARNING错误
4、mysql_query()
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
$query查询字符串不应该以分号结束
$link_identifier如果不指定处理方式与mysql_select_db()相同。
返回值:
mysql_query() 仅对 SELECT,SHOW,DESCRIBE, EXPLAIN 和其他语句 语句返回一个 resource,如果查询出现错误则返回 FALSE
。对于其它类型的 SQL 语句,比如INSERT, UPDATE, DELETE, DROP 之类, mysql_query() 在执行成功时返回 TRUE
,出错时返回 FALSE
。返回的结果资源应该传递给 mysql_fetch_array() 和其他函数来处理结果表,取出返回的数据。假定查询成功,可以调用 mysql_num_rows() 来查看对应于 SELECT 语句返回了多少行,或者调用mysql_affected_rows() 来查看对应于 DELETE,INSERT,REPLACE 或 UPDATE 语句影响到了多少行。如果没有权限访问查询语句中引用的表时,mysql_query() 也会返回 FALSE
。
对于包含二进制数据的查询,你必须使用mysql_real_query()而不是mysql_query(),因为二进制代码数据可能包含“\0”字符,而且,mysql_real_query()比mysql_query()更快,因为它不会在查询字符串上调用strlen()。如果查询成功,函数返回零。如果发生一个错误,函数返回非零
5、mysql_affected_rows()
int mysql_affected_rows ([ resource $link_identifier = NULL ] )
取得最近一次与 link_identifier
关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。
6、mysql_fetch_array()
array mysql_fetch_array ( resource $result [, int $ result_type ] )
将结果以数组方式返回,一次只返回一行,然后指向下一行(用while循环取出)如果没有更多结果返回false。如果结果中有两个或两个以上的列有相同的字段名,最后一列将优先。如果sql中指定了别名则使用别名。PHP手册中指出mysql_fetch_array并不明显比mysql_fetch_row慢。
$result_type:可选:MYSQL_ASSOC(关联数组),MYSQL_NUM(枚举数组) 和 MYSQL_BOTH,默认值MYSQL_BOTH。
// MYSQL_NUM $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { printf ("ID: %s Name: %s", $row[0], $row[1]); } // MYSQL_ASSOC $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf ("ID: %s Name: %s", $row["id"], $row["name"]); } // MYSQL_BOTH $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf ("ID: %s Name: %s", $row[0], $row["name"]); }
7、其他函数:
string mysql_error ([ resource $link_identifier ] )
如果没有指定数据库连接则使用上一个连接,只返回最近一次mysql函数的错误文本。
bool mysql_set_charset ( string $charset [, resource $link_identifier = NULL ] )
设置字符集,数据库连接的选择与mysql_select_db()一样。
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )
转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集(这是与mysql_escape_string()的不同)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

When developing websites using PHP, database operations are very common. MySQLi is an extension commonly used in PHP to operate MySQL databases. It provides a relatively complete object-oriented interface, procedural interface, and supports operations of prepared statements. But sometimes when we use mysqli's prepared statements, we will encounter such an error: PHPFatalerror:Calltoundefinedfunctionmysqli_stmt_bin
