Hello, dear Jetto Net Followers!
In this tutorial, you will learn how to connect to MySQL database using PHP and run a simple query! First of all, we are preparing this tutorial assuming that you have PHP and MySQL on your computer! You can set up a PHP and MySQL server with XAMPP!
First of all, you need to have a database on the MySQL server. You can create a database and a table in the database with the following SQL codes in PhpMyAdmin or MySQL command line:
CREATE DATABASE database_name; // We are creating the database.
USE database_name; // We specify the database to be used.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, // primary key and an id field to be auto incremented
name VARCHAR(30) NOT NULL, // name field
email VARCHAR(50) // email field
);
INSERT INTO users (name, email) VALUES ('Ahmet', '[email protected]');
Display More
Yes, when we run the code above, we will have a database and a table with three fields in the database. Now we can move on to creating the connection with PHP:
<?php
$serverName = “localhost”; // Database server
$userName = “root”; // Authorized user in the database
$password = “”; // Password of the user in the database
$dataBaseName = “database_name”; // Database name
// Database connection
$conn = new mysqli($serverName, $userName, $password, $dataBaseName); // Create a new object with database information from the MySqli library.
// With the control block, we print to the screen in case of an error during the connection.
if ($conn->connect_error) {
die("Connection error: ” . $conn->connect_error);
}
echo “Connection successful!”;
?>
Display More
The PHP code above connects to the database_name database on the localhost server with the root user. If the connection is successful, the message “Connection successful!” is displayed. Otherwise, an error message is displayed.
We have made a database connection with PHP. Now you can perform any operation you want through the $conn connection variable. In this article, I will only show you how to retrieve the data stored in the database. You can easily do it for other queries (SQL knowledge required).
Now let's pull the saved data from the table in the database:
<?php
$sql = "SELECT id, name, email FROM users"; // We write a sql query and assign it to the variable.
$result = $conn->query($sql); // We run the query with the connection variable and assign it to the result variable.
// We check if there is data with a control block.
if ($result->num_rows > 0) {
// If the result is greater than 0, there is data and we print the elements from the query in the result variable using a while loop.
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
// If the result returns 0 or less, there is no data. We report this with a message.
echo "Data not found";
}
?>
Display More
The PHP code above pulls the data from the id, name and email columns in the users table and prints it to the screen.
In this tutorial, you learned how to connect to a MySQL database using PHP and how to perform a simple data extraction query. With this basic knowledge, you can start developing dynamic web applications. You can also ask your questions in the comments!
Happy coding!