search
HomeBackend DevelopmentPHP TutorialHow to call three databases with PHP (1)_PHP tutorial

How to call three databases with PHP (1)_PHP tutorial

Jul 21, 2016 pm 04:05 PM
mysqlphprightcompactdatabasemethodyesserveroftransfersoftware

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
$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
echo "" . $i . " ";

}

echo $pageno . " ";

for($i++;$i
echo " ";

}

if($pageno
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
$fields[$i] = mysql_field_name($rst,$i);//Get the first Name of i+1 fields

$i++;

}

echo "


echo "";

reset($fields);

while(list(,$field_name)=each ($fields)){//Display field name

echo "";

}

echo "> ;";

while($row=mysql_fetch_array($rst)){//Display data on this page

echo "";

reset($ fields);

while(list(,$field_name)=each($fields)){//Display the value of each field

$field_value = $row[$field_name];

if($field_value==""){

echo "";

}

else{

echo "";

}

}

echo "";

}

echo "
$field_name
$field_value
";//End of table output

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

?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315540.htmlTechArticleMySQL 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 a variety of...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software