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

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool