Home > Article > Backend Development > MySQL connection file shortcut in PHP
Many website owners use PHP to enhance the functionality of their web pages. When they combine PHP with the open source relational database MySQL, the feature list grows significantly. They can establish login credentials, conduct user surveys, set and access cookies and sessions, rotate banner ads on their website, host user forums and open online stores, and many other functions that would not be possible without a database of.
MySQL and PHP are compatible products and are often used together by website owners. MySQL code can be included directly in PHP scripts. Both reside on your web server and most web servers support them. Server-side location provides reliable security for the data used by your website.
Connect multiple web pages to one MySQL database
If you have a small website, you might not mind typing the MySQL database connection for a few pages in a PHP script code. However, if your website is large and many pages require access to your MySQL database, you can use shortcuts to save time. Put the MySQL connection code in a separate file and call the saved file where needed.
For example, use the following SQL code in a PHP script to log in to the MySQL database. Save this code in a file called datalogin.php.
<?php // 连接到我的数据库 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("Database_Name") or die(mysql_error()); ?>
Now, whenever you need to connect one of your web pages to the database, in your PHP page you can include this line in that page's file:
MySQL Database Connect include 'datalogin.php';
When your page connects to database, they can read data from it or write information to it. Now you can call MySQL and use it to set up an address book or click counter for your website
Recommended reference study: "mysql tutorial"
The above is the detailed content of MySQL connection file shortcut in PHP. For more information, please follow other related articles on the PHP Chinese website!