How to call three databases with PHP (1)_PHP tutorial
MySQL is a small and exquisite database server software, which is very ideal for small and medium-sized application systems. In addition to supporting standard ANSI SQL statements, the most important thing is that it also supports multiple platforms. On Unix/Linux systems, MySQL supports multi-threaded operation, which can achieve very good performance. Like PHP and Apache, it is an open source software. Its official website is: http://www.mysql.com, which provides source code downloads for Windows, Linux, and Unix versions.
Note that MySQL access functions require corresponding permissions to run. Commonly used related functions are introduced as follows:
(1)integer mysql_connect(host, username, password);
This function starts a connection to the MySQL database on the specified host. If the database is on a different port, add a colon and the port number after the host name. All parameters are optional and by default correspond to the local host, the name of the script being executed by the user, and empty. The host can be an IP address or a domain name.
At the end of script execution, the connection is automatically closed, or it can be closed in advance using mysql_close.
(2)boolean mysql_create_db (database name);
Create a database. Note that the connection must be opened with an account with permission to create databases.
(3)boolean mysql_select_db (database name, connection number);
Select the default database.
(4)integer mysql_query (SQL statement, connection number);
Query the specified database. If the SQL statement is select, a result number is returned, otherwise the returned value can be ignored. If it fails, return false.
(5)array mysql_fetch_array (result number);
Fetch the next row and return an array. It can be accessed with numeric subscript (the first field is subscript 0), or with String subscript access (that is, using each field name). If the last row has been fetched, return false.
(6)mysql_fetch_row(result number);
Returns a matrix representing all fields of a row in the result set. Each call produces the next row, returning false until there are no rows left. Each field value is indexed by a zero-based offset. This is the fastest way to get results from a query.
(7)integer mysql_num_rows(result number);
Return the number of rows in the result set
(8)integer mysql_num_fields(result number);
Returns the number of fields in the result set.
(9)integer mysql_list_dbs();
Query the server for the database list. It returns a result pointer that can be used with the mysql_fetch_row function and similar functions.
(10)mysql_list_tables(database name);
Get a result pointer pointing to the form list of the specified database. This result pointer can be used by any function that obtains rows from the result set.
(11)mysql_close(connection number);
Close the connection to the database. The connection must be opened by mysql_connect. The use of this function is not strictly necessary since all non-permanent links are automatically closed at the end of the script.
(12) mysql_pconnect (host, username, password);
is completely similar to mysql_connect, but establishes a "permanent connection". Once established, the connection will never be closed, even if the mysql_close function is used Or the program is not closed after execution. The next time you try to establish a permanent connection, if the system finds that a permanent connection already exists, it will directly return the connection number without re-creating it.
The following is an example of calling a MYSQL database and displaying it in pages.
$pagesize = 5; //Display 5 records per page
$host="localhost";
$user= "user";
$password="psw";
$dbname="book"; //The name of the database table being queried;
//Connect to the MySQL database
mysql_connect("$host","$user","$password") or die("Unable to connect to the MySQL database server!");
$db = mysql_select_db("$dbname ") or die("Unable to connect to the database!");
$sql = "select count(*) as total from pagetest";//Generate the SQL statement to query the number of records
$ rst = mysql_query($sql) or die("Unable to execute SQL statement: $sql!"); //Number of query records
$row = mysql_fetch_array($rst) or die("No more records ! "); /Retrieve one record
$rowcount = $row["total"];//Retrieve the number of records
mysql_free_result($rst) or die("Cannot release result resource! "); //Release result resources
$pagecount = bcdiv($rowcount+$pagesize-1,$pagesize,0);//Calculate how many pages there are in total
if(!isset( $pageno)) {
$pageno = 1; //When pageno is not set, the default is to display page 1
}
if($pageno<1 ) {
$pageno = 1; //If pageno is smaller than 1, set it to 1
}
if($pageno>$pagecount) {
$pageno = $pagecount; //If pageno is larger than the total number of pages, set it to the last page
}
if($pageno>0) {
$href = eregi_replace("%2f","/",urlencode($PHP_SELF));//Convert $PHP_SELF into a string that can be used on the URL, so that Chinese can be processed Directory or Chinese file name
if($pageno>1){//Display the link to the previous page
echo "Previous page ";
}
else{
echo "Previous page ";
}
for($i=1;$i<$pageno;$i++){
echo "" . $i . " ";
}
echo $pageno . " ";
for($i++;$i<=$pagecount;$i++){
echo " ";
}
if($pageno<$pagecount){//Display the link to the next page
echo "Next page ";
}
else{
echo "next page";
}
$offset = ($pageno-1) * $pagesize;//Calculate this page The position of the first record in the entire table (the first record is 0)
$sql = "select * from pagetest LIMIT $offset,$pagesize";//Generate the SQL statement to query the data on this page
$rst = mysql_query($sql);//Query the data on this page
$num_fields = mysql_num_fields($rst);//Get the total number of fields
$i = 0;
while($i<$num_fields){//Get the names of all fields
$fields[$i] = mysql_field_name($rst,$i);//Get the first Name of i+1 fields
$i++;
}
echo "
$field_name | ";|
---|---|
"; | $field_value | ";
mysql_free_result($rst) or die("Cannot release result resource! ");//Release result resource
}
else{
echo "There is currently no data in this table! ";
}
mysql_close($server) or die("Unable to disconnect from the server!");//Disconnect and release resources
?> ;

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



PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
