Home Backend Development PHP Tutorial MYSQL common functions in PHP are necessary to operate the database under PHP

MYSQL common functions in PHP are necessary to operate the database under PHP

Jul 29, 2016 am 08:43 AM
mysql name query resource

1. mysql_connect()-Establish a database connection
Format:
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
Note: The connection must be closed when using this connection
2. mysql_pconnect()-Establish a database connection
Format:
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
Example:
$conn = @mysql_pconnect("localhost", "username ", "password") or dir("Cannot connect to Mysql Server");
Explanation: Using this connection function does not require explicit closing of the connection. It is equivalent to using a connection pool
3. mysql_close()-Close the database connection
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("Cannot select this database, Or the database does not exist");
echo "You have connected to the MyDatabase database";
mysql_close();
4. mysql_select_db()-Select database
Format:
boolean mysql_select_db(string db_name [, resource link_id])
Example:
$conn = @mysql_connect("localhost", "username", "password") or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("Cannot select this database or database Does not exist");
5. mysql_query()-Query MySQL
Format:
resource mysql_query (string query, [resource link_id])
Example:
$linkId = @mysql_connect("localhost", "username", "password" ) or die("Cannot connect to Mysql Server");
@mysql_select_db("MyDatabase") or die("This database cannot be selected, or the database does not exist");
$query = "select * from MyTable";
$ result = mysql_query($query);
mysql_close();
Note: If the SQL query is executed successfully, the resource identifier is returned, and FALSE is returned if it fails. If the update is executed successfully, TRUE is returned, otherwise FALSE is returned.
6. mysql_db_query()-Query MySQL
Format:
resource mysql_db_query(string database, string query [, resource link_id])
Example:
$linkId = @mysql_connect(" localhost", "username", "password") or die("Cannot connect to MysqlServer");
$query = "select * from MyTable";
$result = mysql_db_query("MyDatabase", $query);
mysql_close( ; query = "select id, name from MyTable order by name";
$result = mysql_query($query);
for($count=0;$count<=mysql_numrows($result);$count++)
{
$c_id = mysql_result($result, 0, "id");
$c_name = mysql_result($result, 0, "name");
echo $c_id,$c_name;
}
Explanation: The simplest and least efficient data Get function
8, mysql_fetch_row() - Get and display data
Format:
array mysql_fetch_row (resource result_set)
Example:
$query = "select id, name from MyTable order by name";
$result = mysql_query($query );
while (list($id, $name) = mysql_fetch_row($result)) {
echo("Name: $name ($id)
");
}
Description: The function is from result_set Get the entire row of data and place the values ​​in an indexed array. Usually the list() function is used to obtain and display data. Format:
array mysql_fetch_array (resource result_set [, int result_type])
Example:
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["id"];
$name = $row["name "];
echo "Name: $name ($id)
";
}
Another example:
$query = "select id, name from MyTable order by name";
$result = mysql_query( $query);
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$id = $row[0];
$name = $row[1];
echo "Name: $name ($id)
";
}
Explanation:
result_type values ​​are:
MYSQL_ASSOC: The field name represents the key, and the field content is the value
MYSQL_NUM: Numeric index array, the operation is the same as the mysql_fetch_ros() function
MYSQL_BOTH: That is, as Associative arrays are returned as numerically indexed arrays. The default value of result_type.
10. mysql_fetch_assoc()-Get and display data
Format:
array mysql_fetch_assoc (resource result_set)
Equivalent to calling mysql_fetch_array(resource, MYSQL_ASSOC);
11. mysql_fetch_object()-Get and display data
Format:
object mysq l_fetch_object( resource result_set)
Example:
$query = "select id, name from MyTable order by name";
while ($row = mysql_fetch_object($result)) {
$id = $row->id;
$name = $row->name;
echo "Name: $name ($id)
";
}
Description: Returns an object, which is the same as mysql_fetch_array() in operation
12. mysql_num_rows()- The number of selected records
Format:
int mysql_num_rows(resource result_set)
Example:
query = "select id, name from MyTable where id > 65";
$result = mysql_query($query);
echo " There are ".mysql_num_rows($result)." records whose IDs are greater than 65";
Note: This is only useful when determining the number of records obtained by the select query.
13. mysql_affected_rows() - The number of records affected by Insert, update, delete
Format:
int mysql_affected_rows([resource link_id])
Example:
$query = "update MyTable set name='CheneyFu' where id> =5";
$result = mysql_query($query);
echo "The number of updated records with names whose ID is greater than or equal to 5:".mysql_affected_rows();
Description: This function obtains the update statement affected by INSERT, UPDATE or DELETE Number of rows affected
14, mysql_list_dbs()-Get database list information
Format:
resource mysql_list_dbs([resource link_id])
Example:
mysql_connect("localhost", "username", "password");
$dbs = mysql_list_dbs();
echo "Databases:
";
while (list($db) = mysql_fetch_rows($dbs)) {
echo "$db
";
}
Description : Display all database names
15. mysql_db_name()-Get the database name
Format:
string mysql_db_name(resource result_set, integer index)
Description: This function gets the database name located in the specified index index in the result_set returned by mysql_list_dbs()
16. mysql_list_tables()-Get the list of database tables
Format:
resource mysql_list_tables(string database [, resource link_id])
Example:
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables( "MyDatabase");
while (list($table) = mysql_fetch_row($tables)) {
echo "$table
";
}
Description: This function gets the table names of all tables in the database
17. mysql_tablename()-Get the name of a database table
Format:
string mysql_tablename(resource result_set, integer index)
Example:
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables( "MyDatabase");
$count = -1;
while (++$count < mysql_numrows($tables)) {
echo mysql_tablename($tables, $count)."
";
}
Description: This function gets the table name located at the specified index in the result_set returned by mysql_list_tables()
18. mysql_fetch_field()-Get field information
Format:
object mysql_fetch_field(resource result [, int field_offset])
Example:
mysql_connect ("localhost", "username", "password");
mysql_select_db("MyDatabase");
$query = "select * from MyTable";
$result = mysql_query($query);
$counts = mysql_num_fields($ result);
for($count = 0; $count < $counts; $count++) {
$field = mysql_fetch_field($result, $count);
echo "

$field->name $field ->type ($field->max_length)

";
}
Description:
The returned object has a total of 12 object attributes:
name: field name
table: table where the field is located
max_length: maximum length of the field
not_null: 1 if the field cannot be null, otherwise 0
primary_key: 1 if the field is the primary key, 0 otherwise
unique_key: 1 if the field is a unique key, 0 otherwise
multiple_key: 1 if the field is non-unique, 0 otherwise
numeric: 1 if the field is numeric 1, otherwise 0
blob: 1 if the field is a BLOB, 0 otherwise
type: The data type of the field
unsigned: 1 if the field is an unsigned number, 0 otherwise
zerofill: Zero-fill if the field is ” is 1, otherwise it is 0
19. mysql_num_fields()-Get the number of fields queried
Format:
integer mysql_num_fields(resource result_set)
Example:
$query = "select id,name from MyTable order by name";
$result = mysql_query($query);
echo "The number of fields in this query is: ".mysql_num_fields($result)."
";
20. mysql_list_fields()-Get all fields of the specified table Field name
Format:
resource mysql_list_fields (string database_name, string table_name [, resource link_id])
Example:
$fields =mysql_list_fields("MyDatabase", "MyTable");
echo "The number of fields in the table MyTable in the database MyDatabase : ".mysql_num_fields($fields)."
";
21. mysql_field_flags()-Get the specified field options
Format:
string mysql_field_flags (resource result_set, integer field_offset)
Example:
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
$row=mysql_fetch_wor($row);
22. mysql_field_len()-Get the maximum length of the specified field
Format:
integer mysql_field_len (resource result_set, integer field_offset)
Example:
$query = "select name from MyTable";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_len($result, 0). "& Lt; br /& gt;";
Explanation:
If mysql_field_len ($ reselt, 0) = 16777215
then numer_Format (mysql_field_len ($ Result)) is equal to 16,777,215
23, l_field_name () -Capor field name
format :
string mysql_field_name (resource result_set, int field_offset)
Example:
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result ; , name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_type($result, 0); // Result: int
25, mysql_field_table() -Get the name of the table where the field is located
Format:
string mysql_field_table (resource result_set, int field_offset)
Example:
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_table($result, 0); // Result: MyTable
The above has introduced the common MYSQL functions in PHP that are necessary to operate the database under PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles