PHP4 与 MySQL 数据库操作函数详解(1)_MySQL
说PHP就不能不提MySQL,而要讲MySQL,那么PHP也是必然要被提起。PHP的迅速崛起,离不开MySQL,而MySQL的广泛应用,也与PHP休戚相关。
下面详细分析PHP4中与MySQL相关操作的函数(共32个,开头都为mysql_):
. 连接数据库服务器(database server)的函数(2个):
(1).mysql_connect()
格式:int mysql_connect(string [hostname] [ort],string [username],string [password]);
参数中的port参数表示数据库服务器的端口号,一般用它的默认端口号就可以了。
如果不填任何参数,则默认的hostname为localhost,username为root,password为空。
函数执行成功,返回一个int 类型的连接号(link_identifier),执行失败,返回false值。
例子:
$connect = mysql_connect("localhost","user","password");
if($connect) echo "Connect Successed!"; //连接成功,显示Connect Successed!
else echo "Connect Failed!"; //连接失败,显示Connect Failed!
?>
在上例中,如mysql_connect()执行失败,将显示系统的错误提示,而后继续往下执行。那,该如何屏蔽这些系统的错误提示并在失败后结束程序?
在MySQL中,允许在数据库函数之前加上@符号,屏蔽系统的错误提示,同时用die()函数给出更易理解的错误提示,然后die()函数将自动退出程序。
上例可以改为:
$connect = @mysql_connect("localhost","user","password") or die ("Unable to connect database server!");
?>
如mysql_connect()执行失败,将显示 Unable to connect database server!后,退出程序。
(2).mysql_pconnect()
格式:int mysql_pconnect(string [hostname] [ort],string [username],string [password]);
此函数与(1)的mysql_connect()基本相同,区别在于:
--------- 当数据库操作结束之后 ,由(1)的mysql_connect()建立的连接将自动关闭,而(2)的mysql_pconnect()建立的连接将继续存在,是一种稳固持久的连接。
--------- 在(2)的mysql_pconnect(),每次连接前,都会检查是否有使用同样的hostname,use,password的连接,如果有,则直接使用这个连接号。
--------- (1)的mysql_connect()建立的连接可以用mysql_close()关闭,而(2)的mysql_pconnect()不能用mysql_close()来关闭。
.关闭数据库连接函数(1个):
mysql_close()
格式:int mysql_close(int link_identifier);
关闭由mysql_connect()函数建立的连接,执行成功,返回ture值,失败则返回false值。
例子如下:
$connect = @mysql_connect("hostname","user","password") or die("Unable to connect database server!");
$close = @mysql_close($connect) or die ("Unable to close database server connect!");
?>
注:mysql_close()不能关闭由mysql_pconnect()函数建立的连接。
.选择数据库函数(1个):
mysql_select_db()
格式:int mysql_select_db(string database name , int link_identifier);
选择指定的database name ,成功,返回1个真值(True),失败,则返回1个False值
例子1:
$select = mysql_select_db('forum' , $connect);
if($select)
{echo "connect db forum successed!";}
else
{echo "connect db forum failed!";}
?>
例子2:
$select = mysql_select_db("forum",$connect) or die("Can not connect this DB!");
?>
注:此函数相当于在MySQL中的USE语句:如 USE forum
.SQL查询函数(2个):
1、mysql_query()
格式:int mysql_query(string sqlquery , int link_identifier);
向服务器发一段标准SQL语句请求。如果失败,则返回一False值。
例子:
$connect = mysql_connect($hostname,$user,$pwd);
$select = mysql_select_db($dbname,$connect);
$query = mysql_query($sql , $connect);
if($query) echo "Successed !";
else echo "Failed !";
?>
此函数一定要与mysql_select_db()函数配合使用,单独使用它就没有意义了!
2、mysql_db_query()
格式:int mysql_db_query(string database , string sqlquery , int link_identifier);
在此函数中必须指定数据库名database和SQL语句sqlquery,如失败则返回False。
例子:
$connect = mysql_connect($hostname , $user , $pwd);
$query = mysql_db_query($dbname , $sql , $connect);
if($query) echo "Successed !";
else echo "Failed !";
?>
mysql_db_query()与mysql_query()的区别就在于前者可以不用使用mysql_select_db()来选择数据库database,而在执行SQL语句的同时,进行选择数据库。
转载:www.chinaphp.com

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



Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

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

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
