php mysqlmysqliPDO (1) mysql, mysqlipdo_PHP tutorial
Jul 12, 2016 am 08:56 AMphp 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() 關(guān)閉指定的連接標(biāo)識所關(guān)聯(lián)的到 MySQL 服務(wù)器的非持久連接。如果沒有指定 link_identifier
,則關(guān)閉上一個打開的連接.通常不需要使用 mysql_close(),因為已打開的非持久連接會在腳本執(zhí)行完畢后自動關(guān)閉。PHP 4 Zend 引擎引進(jìn)了引用計數(shù)系統(tǒng),可以自動檢測到一個資源不再被引用了(和 Java 一樣)。這種情況下此資源使用的所有外部資源都會被垃圾回收系統(tǒng)釋放。因此,很少需要手工釋放內(nèi)存。
3、mysql_select_db()
bool mysql_select_db ( string $database_name [, resource $ link_identifier ] )
如果沒有指定$link_identifier則使用上一個打開的數(shù)據(jù)庫連接,如果沒有打開的連接則將無參數(shù)調(diào)用mysql_connet()取得一個連接并使用。如果沒有連接則E_WARNING錯誤
4、mysql_query()
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
$query查詢字符串不應(yīng)該以分號結(jié)束
$link_identifier如果不指定處理方式與mysql_select_db()相同。
返回值:
mysql_query() 僅對 SELECT,SHOW,DESCRIBE, EXPLAIN 和其他語句 語句返回一個 resource,如果查詢出現(xiàn)錯誤則返回 FALSE
。對于其它類型的 SQL 語句,比如INSERT, UPDATE, DELETE, DROP 之類, mysql_query() 在執(zhí)行成功時返回 TRUE
,出錯時返回 FALSE
。返回的結(jié)果資源應(yīng)該傳遞給 mysql_fetch_array() 和其他函數(shù)來處理結(jié)果表,取出返回的數(shù)據(jù)。假定查詢成功,可以調(diào)用 mysql_num_rows() 來查看對應(yīng)于 SELECT 語句返回了多少行,或者調(diào)用mysql_affected_rows() 來查看對應(yīng)于 DELETE,INSERT,REPLACE 或 UPDATE 語句影響到了多少行。如果沒有權(quán)限訪問查詢語句中引用的表時,mysql_query() 也會返回 FALSE
。
對于包含二進(jìn)制數(shù)據(jù)的查詢,你必須使用mysql_real_query()而不是mysql_query(),因為二進(jìn)制代碼數(shù)據(jù)可能包含“\0”字符,而且,mysql_real_query()比mysql_query()更快,因為它不會在查詢字符串上調(diào)用strlen()。如果查詢成功,函數(shù)返回零。如果發(fā)生一個錯誤,函數(shù)返回非零
5、mysql_affected_rows()
int mysql_affected_rows ([ resource $link_identifier = NULL ] )
取得最近一次與 link_identifier
關(guān)聯(lián)的 INSERT,UPDATE 或 DELETE 查詢所影響的記錄行數(shù)。
6、mysql_fetch_array()
array mysql_fetch_array ( resource $result [, int $ result_type ] )
將結(jié)果以數(shù)組方式返回,一次只返回一行,然后指向下一行(用while循環(huán)取出)如果沒有更多結(jié)果返回false。如果結(jié)果中有兩個或兩個以上的列有相同的字段名,最后一列將優(yōu)先。如果sql中指定了別名則使用別名。PHP手冊中指出mysql_fetch_array并不明顯比mysql_fetch_row慢。
$result_type:可選:MYSQL_ASSOC(關(guān)聯(lián)數(shù)組),MYSQL_NUM(枚舉數(shù)組) 和 MYSQL_BOTH,默認(rèn)值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、其他函數(shù):
string mysql_error ([ resource $link_identifier ] )
如果沒有指定數(shù)據(jù)庫連接則使用上一個連接,只返回最近一次mysql函數(shù)的錯誤文本。
bool mysql_set_charset ( string $charset [, resource $link_identifier = NULL ] )
設(shè)置字符集,數(shù)據(jù)庫連接的選擇與mysql_select_db()一樣。
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )
????轉(zhuǎn)義 SQL 語句中使用的字符串中的特殊字符,并考慮到連接的當(dāng)前字符集(這是與mysql_escape_string()的不同)

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

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())

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

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
