Home Blog How to create a php file connects to the database in PhpMyAdmin

How to create a php file connects to the database in PhpMyAdmin

When building an Android / IOS application that connects to the PhpMyAdmin Database, you have to create a connection string that can access, write or edit the information stored there.

However, if you declare a connection string on each file, it will make things complicated every times you change. Instead, you can create a separate file that holds the connection information to the database and invokes it from other files when used.

How to create a php file connects to the Database in PhpMyAdmin

To create a connection to the Database you will need the following information:

  • host: is the name / address of the host, where the database is stored.
  • username: account name allows use of Database.
  • password: username password.
  • database: database connection name.

After you have the information above, you will build a separate connection file as follows:

For example: create the db-connect.php file with the following content and save it to the directory on your host.

mysqli_query($conn,"SET NAMES 'utf8'");
if (mysqli_connect_error()) 
{ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
{ echo "Success to connect to MySQL"; } 

Then, you can check again using the Web browser. As you can see, the above command will be show message like:

  • Failed to connect to MySQL: if the connection fails.
  • Success to connect to MySQL: if the connection is successful.


And to call using this connection file from other files you can declare as follows:

include "db-connect.php";
$query="SELECT * FROM category ORDER BY idcategory";
$data=mysqli_query($conn, $query);

Above is how to create a file to connect to PhpMyAdmin Database and you need customize the connection to your own Database. Hope the post is useful to you.


Please enter your comment!
Please enter your name here