MySql tutorial

<< Prev
Next >>

Introduction

MySql is the most popular Open Source SQL database management system.

A database is an application that stores a collection of data. MySQL works on many operating systems and with many languages including PHP, PERL, C++, JAVA, etc.
MySQL Server today offers connectivity, speed, and security.
MySql is available on Windows, Linux and MAC OS X and provides a visual console to easily administer MySql environments and gain better visibility into databases.


Install MySql Server in Linux

The easiest way to install MySql is to use the MySql repositories. For APT-based distribution like Debian and Ubuntu, follow the instrution:


sudo apt-get update


Install MySql by the following command:


sudo apt-get install mysql-server


The MySql server is started automatically after installation. You can check the status of the MySql server with the following command:


sudo service mysql status


Stop the MySql server with the following command:


sudo service mysql stop


Start the MySql server with the following command:


sudo service mysql start


Install MySql Server in Windows

The recommended way to install MySql server on Microsoft Windows is to use the MySql installer. Complete the installation process by following the instructions.
This will install several MySql products and start MySql server.
A more simple way to test MySql database is to use XAMPP application. XAMPP is a very easy to install Apache Distributions for Linux, Solaris, Windows and MAC OS X.
The package includes the Apache web server, MySql, PHP, PERL, a FTP server and phpMyAdmin.
XAMPP Control Panel allows you to manage these services.


MySql

PhpMyAdmin is a popular web-based administration tool used to administrate MySql servers and databases.
Using PhpMyAdmin, we can:
- Create users and privileges:


MySql

- Create databases:


MySql

- Create tables:


MySql

Before we can access data in the MySQL database, we need to be able to connect to the server.
Example php connect to MySql database (connect.php):


<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$database = "name_db";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo"Connected successfully";
?>


MySql

Example to added records to MySql database (add.php):


<?php
if(isset($_POST['save']))
{
$sql = "INSERT INTO users (username, password, email)
VALUES ('".$_POST["username"]."',
'".$_POST["password"]."','".$_POST["email"]."')";
$result= mysqli_query($conn,$sql);
}
?>


And the html form (add.php):


<form action="add.php" method="post">
First name:
<input type= "text" name ="username">
Password:
<input type= "text" name ="password">
Email:
<input type= "text" name ="email">
lt;button type= "submit" name="save">SaveSave</button>
</form>


MySql

Example to retrieve data from MySql database (add.php):


<?php
$sql= "SELECT username, password, email FROM users";
$result= $conn->query($sql);
if( $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["username"]. " - Password: " . $row["password"]. " Email" . $row["email"]. "
";
}
}
else {
echo"0 results";
}
$conn->close();
?>


<< Prev
Next >>

Visit my websites:

https://www.jwebsaints.com
https://www.jwebplants.com