1. LARAVEL
Connecting your Laravel project to MySQL database
Connecting your laravel project to the MySQL database is moderately easy. Since we have done the installation with XAMPP. It comes along with already installed MySQL and PHPMyAdmin. In this tutorial, I will make use of phpMyAdmin to create a new database that will connect to the laravel application. This post is assuming you already have a laravel project setup in your local and you are able to access its homepage via localhost or custom set domain name.
Step1: Create a new database via phpmyadmin
Navigate to domain name.dev/ PHPMyAdmin, Click on the Databases tab and create a new database with your wanted name.
Clicking on create will create a new database in your XAMPP MySQL.
Step2: Changes in.env configuration file
Once the database is created, you need to tell your laravel project the details about the database.
Laravel 5 has a pretty simple way to do that, All configuration that you should stay private and should not share goes into the .env file of your laravel project.
DB_CONNECTION=mysql DB_HOST=190.0.0.1 DB_PORT=4008 DB_DATABASE=testProject DB_USERNAME=root DB_PASSWORD=
Modify the following property in your .env file according to your database settings.
Step3: Run migration (optional)
To test if you've got with success combined your Laravel project to the database you'll run the migration command. Laravel project by default comes with some default table for storing users and their password requests. once you run migrate command you it ought to produce the default tables for you within the database.
php artisan migrate
That’s it! You are now ready to make database operations in your project.
2.PHP
PHP Connect to MySQL Server
In this tutorial you will learn how to connect to the MySQL server using PHP.
Ways of Connecting to MySQL through PHP
In order to store or access the data inside a MySQL database, you first need to connect to the MySQL database server. PHP offers two different ways to connect to MySQL server: MySQLi (Improved MySQL) and PDO (PHP Data Objects) extensions.
While the PDO extension is more portable and supports more than twelve different databases, MySQLi extension as the name suggests supports MySQL database only. MySQLi extension however provides an easier way to connect to, and execute queries on, a MySQL database server. Both PDO and MySQLi offer an object-oriented API, but MySQLi also offers a procedural API which is relatively easy for beginners to understand.
Tip: The PHP's MySQLi extension provides both speed and feature benefits over the PDO extension, so it could be a better choice for MySQL-specific projects.
Connecting to MySQL Database Server
In PHP you can easily do this using the
mysqli_connect()
function. All communication between PHP and the MySQL database server takes place through this connection. Here're the basic syntaxes for connecting to MySQL using MySQLi and PDO extensions:Syntax: MySQLi, Procedural way
$link = mysqli_connect("hostname", "username", "password", "database");
Syntax: MySQLi, Object Oriented way
$mysqli = new mysqli("hostname", "username", "password", "database");
Syntax: PHP Data Objects (PDO) way
$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");
The hostname parameter in the above syntax specify the host name (e.g.
localhost
), or IP address of the MySQL server, whereas the username and password parameters specifies the credentials to access MySQL server, and the database parameter, if provided will specify the default MySQL database to be used when performing queries.
The following example shows how to connect to MySQL database server using MySQLi (both procedural and object oriented way) and PDO extension.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Print host information
echo "Connect Successfully. Host info: " . mysqli_get_host_info($link);
?>
Note: The default username for MySQL database server is
root
and there is no password. However to prevent your databases from intrusion and unauthorized access you should set a password for MySQL accounts.
Tip: Setting the
PDO::ATTR_ERRMODE
attribute to PDO::ERRMODE_EXCEPTION
tells PDO to throw exceptions whenever a database error occurs.Closing the MySQL Database Server Connection
The connection to the MySQL database server will be closed automatically as soon as the execution of the script ends. However, if you want to close it earlier you can do this by simply calling the PHP
mysqli_close()
function.Example <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Print host information
echo "Connect Successfully. Host info: " . mysqli_get_host_info($link);
// Close connection
mysqli_close($link);
?>
0 Comments
CAN FEEDBACK
Emoji