Home >Backend Development >PHP Tutorial >Reasonable and effective combination of PHP and MYSQL database_PHP tutorial
By combining PHP and MYSQL reasonably and effectively, you can create an exquisite database website. MYSQL is a small, compact data server that supports standard SQL. It can be used in both UNIX and WINDOWS environments.
PHP and MYSQL are both free and open source. Their combination can be developed in WINDOWS and provided services in UNIX. PHP also supports some other databases including PostgreSQL.
Here is an example:
First you have installed PHP and MYSQL. This simple script example reads data from the database and displays it. .
<html><body><?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb", $db); $result = mysql_query("SELECT * FROM books",$db); echo "Title: ".mysql_result($result,0,"title")."<br>n"; echo "Author: ".mysql_result($result,0,"author")."<br>n"; echo "Price: ".mysql_result($result,0,"price")."<br>n"; ?></body></html> |
The function mysql_connect() refers to connecting to a MYSQL server through the user name (or the specified password) on the specified host. The variable $db is used to submit this connection. .
mysql_select_db() specifies the database to be used for subsequent questions. The function mysql_query() is to send a SQL query to MySQL for execution, and the result is returned and stored in the variable $result.
Finally, the mysql_result() function is to get the result.