PHP中mysql_connect()和mysql_pconnect()函数的区别
mysql_connect()函数: mysql_connect()函数用于与数据库建立新的连接。此连接在脚本开始执行时建立。与数据库建立此连接后,直到脚本执行后才有效或与数据库连接。这意味着一旦脚本停止执行,与数据库的连接也将关闭。 mysql_close()方法用于关闭与数据库的连接。
示例 1:在下面的代码中,如果连接成功,它将显示 echo 部分,如果出现任何错误,它将显示 die 部分。
PHP
PHP
PHP
PHP
Database Connection Established Successfully.
示例 2:在以下代码中,我们连接到 geeksforgeeks.org 数据库的 3307 端口。
PHP
Connected successfully to GFG Database
mysql_pconnect()函数: mysql_pconnect()是一种与mysql_connect() 稍有不同的函数。当您使用此函数连接到数据库时,它将搜索是否存在使用相同用户名和密码与数据库建立的任何其他现有连接,如果此条件为真,则返回资源 ID。每当调用脚本时它不会一次又一次地建立连接,并且当脚本停止执行时它也不会结束连接。这种类型的连接称为持久连接。
示例 1:在以下代码中,我们使用 mysql_pconnect()函数建立持久连接。
PHP
Persistent Connection Established
示例 2:在以下代码中,我们使用持久连接 (mysql_pconnect()) 连接到端口 3307 上的 geeksforgeeks.org 数据库。
PHP
Connected successfully to GFG Database using Persistent Connection
mysql_connect() 和 mysql_pconnect() 函数的区别: mysql_connect() function mysql_pconnect() functionThis function establishes a connection with the database when the script is called. This function first checks whether a connection with the same username and password is created or not and if not then it establishes a connection. The mysql_close() method is used to close the connection with the database. The mysql_close() does not close the connection with the database. It is a time-consuming function as it establishes a connection every time it is being called. It is a time-saving function as it does not establish a connection every time it is called rather connection establishment is performed only once. It is used when a new connection is to be established. It is used when we don’t want to disconnect from the database and keep it for future use. It requires more memory as the connection is established every time this function is called. It requires less memory as the connection is established only once Due to its complex use, it is not much user-friendly Due to its simplicity, it is more user-friendly. The database is opened every time the page is loaded using myql_connect() method. The database is not opened every time the page is loaded with the mysql_pconnect() method.