What is MYSQL


MySQL runs on virtually all platforms, including Linux, UNIX, and Windows. Although it can be used in a wide range of applications, MySQL is most often associated with web-based applications and online publishing and is an important component of an open source enterprise stack called LAMP. LAMP is a Web development platform that uses Linux as the operating system, Apache as the Web server, MySQL as the relational database management system and PHP as the object-oriented scripting language. (Sometimes Perl or Python is used instead of PHP.)
  
  1.  MySQL is a database system used on the web
  2. MySQL is a database system that runs on a server
  3. MySQL is ideal for both small and large applications
 4.  MySQL is very fast, reliable, and easy to use
 5.  MySQL uses standard SQL
 6.  MySQL compiles on a number of platforms
 7.  MySQL is free to download and use
 8.  MySQL is developed, distributed, and supported by Oracle Corporation
  9. MySQL is named after co-founder Monty Widenius's daughter: My
 10.  The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.
   
   Databases are useful for storing information categorically. A company may have a database with the following tables:
   
   Employees
   Products
   Customers
   Orders
         
Download MySQL Database If you don't have a PHP server with a MySQL Database, you can download it for free here:http://www.mysql.com
Here is 5 Major steps to fetch or insert data .

MYSQL Connections


Note: we are taking (MySQLi Procedural) in all Tutorial
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

MYSQL Create Database


<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; // creating database $sql = "CREATE DATABASE Akash"; if (mysqli_query($conn, $sql)) { echo "Database created successfully"; } else { echo "Error creating database: " . mysqli_error($conn); } ?> It will create database name: Akash

Creating Table


<?php $servername = "localhost"; $username = "username"; $password = "password"; $db="Akash" // Create connection $conn = mysqli_connect($servername, $username, $password,$db); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; // sql to create table $sql = "CREATE TABLE Kumar ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table kumar created successfully"; } else { echo "Error creating table: " . $conn->error; } ?> It will create database name: Akash and table name: kumar

Fetching


<?php $servername = "localhost"; $username = "username"; $password = "password"; $db="Akash"; // Create connection $conn = mysqli_connect($servername, $username, $password,$db); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. ""; } } else { echo "0 results"; } ?> we are fetching from table :Kumar method mysqli_fetch_assoc($result) ,you can use mysqli_fetch_all($result) both are almost same first will fetch in key value manner and second one by indexing like array. $row[1].

Closing the connections


like this look simple !!
                                mysqli_close($conn);    
                          
CONTENTS