Home Backend Development PHP Tutorial MYSQL common functions in PHP (necessary for operating databases under PHP)_PHP tutorial

MYSQL common functions in PHP (necessary for operating databases under PHP)_PHP tutorial

Jul 21, 2016 pm 03:34 PM
connect mysql php Down function Commonly used Establish Essential operate database connect

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");
Instructions: Use this connection Close the connection that must be displayed

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 ");
Note: 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("This database cannot be selected, or the 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("Cannot select this database, or the database does not exist");
$query = "select * from MyTable";
$result = mysql_query($query);
mysql_close();
Explanation: 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();
Note: In order to make the code clear, it is not recommended to use this function call

7. mysql_result()-Get and display data
Format:
mixed mysql_result (resource result_set, int row [, mixed field])
Example:
$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;
}
Description: The simplest and least efficient data acquisition function

8. mysql_fetch_row()-obtain 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 gets the entire data row from result_set and puts the value in in an indexed array. Usually the list() function is used

9. mysql_fetch_array()-get 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)
}
Note:
The values ​​of result_type 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: Returned as an associative array and a numerical index array. 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 mysql_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 with IDs greater than 65";
Note: Only when determining the select query It is only useful when the number of records obtained.

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 name with ID greater than or equal to 5 has been updated Number of records: ".mysql_affected_rows();
Description: This function gets the number of rows affected by the INSERT, UPDATE or DELETE update statement

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 obtains the database name located at the specified index in the result_set returned by mysql_list_dbs()

16. mysql_list_tables() - Gets the database table list
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 tables of all tables in the database Name

17. mysql_tablename()-Get a database table name
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 specified index in the result_set returned by mysql_list_tables() Table name

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: the maximum length of the field
not_null: if the field cannot is null, it is 1, otherwise 0
primary_key: If the field is the primary key, it is 1, otherwise 0
unique_key: If the field is the unique key, it is 1, otherwise 0
multiple_key: If the field is Non-unique, 1, otherwise 0
numeric: 1 if the field is numeric, otherwise 0
blob: 1 if the field is BLOB, otherwise 0
type: Data type of the field
unsigned: 1 if the field is an unsigned number, 0 otherwise
zerofill: 1 if the field is "zero filled", otherwise 0

19, mysql_num_fields() -Get the number of fields in the query
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 the field names of all fields in the specified table
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 option
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)."
";
Explanation:
If mysql_field_len($reseult, 0) = 16777215
Then numer_format(mysql_field_len($result)) is equal to 16,777,215

23. mysql_field_name()-Get the 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) ;
echo mysql_field_name($result, 0); // Result: PKID

24. mysql_field_type()-Get field type
Format:
string mysql_field_type ( resource result_set, int field_offset)
Example:
$query = "select id, 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 table name of the field
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322386.htmlTechArticle1. mysql_connect()-Establish database connection format: resource mysql_connect([string hostname [:port] [:/ path/to/socket] [, string username] [, string password]]) Example: $conn = @mys...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Navicat cannot connect to MySQL/MariaDB/PostgreSQL and other databases Navicat cannot connect to MySQL/MariaDB/PostgreSQL and other databases Apr 08, 2025 pm 11:00 PM

Common reasons why Navicat cannot connect to the database and its solutions: 1. Check the server's running status; 2. Check the connection information; 3. Adjust the firewall settings; 4. Configure remote access; 5. Troubleshoot network problems; 6. Check permissions; 7. Ensure version compatibility; 8. Troubleshoot other possibilities.

Navicat connects to database error code and solution Navicat connects to database error code and solution Apr 08, 2025 pm 11:06 PM

Common errors and solutions when connecting to databases: Username or password (Error 1045) Firewall blocks connection (Error 2003) Connection timeout (Error 10060) Unable to use socket connection (Error 1042) SSL connection error (Error 10055) Too many connection attempts result in the host being blocked (Error 1129) Database does not exist (Error 1049) No permission to connect to database (Error 1000)

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to connect to local mysql in navicat How to connect to local mysql in navicat Apr 09, 2025 am 07:45 AM

To connect to a local MySQL database using Navicat: Create a connection and set the connection name, host, port, username, and password. Test the connection to make sure the parameters are correct. Save the connection. Select a new connection from the connection list. Double-click the database you want to connect to.

See all articles