How to connect to MySQL with PHP Print

  • 21

Here's an example code on how to connect to a MySQL database using PHP:

<?php // database credentials $servername = "your-issued-mysql-server-host-name"; $username = "yourusername"; $password = "yourpassword"; $dbname = "yourdatabase"; // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; // close connection mysqli_close($conn); ?>

In this example, you'll need to replace the database credentials with your own.

Once the connection is established, you can execute queries on the database using the $conn object.


Was this answer helpful?

« Back