Home > Backend Development > PHP Problem > How to connect to database using php

How to connect to database using php

藏色散人
Release: 2023-02-28 09:24:01
Original
4485 people have browsed it

How to connect to database using php

How to use php to connect to the database

First, create a new PHP document in our web directory. Name it testMysql.php, you can name your document whatever you want.

How to connect to database using php

Open your own php document, enter the following code to connect to your database.

<?php $link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;); if (!$link) {  die(&#39;Could not connect to MySQL: &#39; . mysql_error()); } else echo &#39;Connection OK&#39;;mysql_close($link); ?>
Copy after login

Of course, the password here in the database is empty, everyone must fill in the correct Mysql password.

How to connect to database using php

Enter the browser, enter the local loop address to connect, and Connection ok will appear! At this point, we have completed the connection to the database.

How to connect to database using php

It is possible that you may find the following error. This is because your PHP version is relatively new. You are required to use the Mysqli function to connect or PDO to connect. This is to ensure that everyone is connected. Database security.

How to connect to database using php

Modify the above d code to

How to connect to database using php

Enter the browser, enter the local loop address to connect, and Connection ok will appear! The above error is gone and the database connection is successful!

How to connect to database using php

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to connect to database using php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template