codescracker


php

PHP and MySQLi Tutorial



« Previous Tutorial Next Tutorial »


Here you will learn all about PHP and MySQLi step by step with examples.

What is PHP and MySQLi

As you already known that PHP is a scripting language and MySQLi is the improved version of the MySQL database.

Why to use PHP with MySQLi

You can use PHP scripting language on your web to connect with your database like MySQL.

If you want to operate your database or use your database to feed, retrieve, update, or delete data in your database from your website or from the web, then go for PHP with MySQLi.

With PHP, everything is possible in case of performing actions over database like MySQL.

PHP and MySQLi Tutorial Topics

This PHP and MySQLi tutorial is divided into following separate parts:

You will learn all the above topics of PHP and MySQLi step by step divided into separate tutorials.

Advantages of using PHP with MySQLi

Here are some advantages of using PHP with MySQLi.

Above are the list of some simple advantages of using PHP with MySQLi as there are a lot of advantages of MySQL using PHP with MySQLi.

Now let's take an example on PHP and MySQLi. If you will not able to understand the below example, then just leave it and go for next tutorial. From next tutorial, you will learn all about PHP and MySQLi step by step. And after giving some time in learning from next tutorial about PHP and MySQLi, you will feel better and can understand the below example.

PHP and MySQLi Example

Now let's take step by step example that will illustrate how to implement MySQL database using PHP with MySQLi.

Here is a simple example of PHP and MySQLi.

Open XAMPP software as shown here in the following figure. This is the window of opened XAMPP

php mysqli

Start the module named Apache (server) and MySQL (database) on clicking on the start button next to both. After starting the apache server and mysql database, your XAMPP window will looks like:

php mysqli example

Now click on the Admin button next to the MySQL's Start button or MySQL's Stop button (after starting the MySQL database, Start button changes into Stop).

After performing the above action, you will be directed to your default browser window that will contains the phpMyAdmin pannel. Here is the screenshot of the phpMyAdmin window.

php mysql phpMyAdmin

Now click on the Database link present at the top banner of the phpMyAdmin window.

After clicking on the Database link, here is the window you will see in your browser:

php mysql example

Now firstly you have to create database before using MySQL Database using PHP with MySQLi.

To create database, just click on the input box of Create Database and enter the Database name say codescracker. Here is the demo screenshot after entering the MySQL database name say codescracker:

php and mysql

Now click on the Create button. Your database will be created after clicking on the create button and here is the demo screenshot that will come out after clicking on the create button.

php mysqli program

This window will ask to enter the table detail (to create table) that will be store in the recently created database.

Here you will enter the name of the table and total number of column that this table will hold.

You can edit the number of column or other details of the table later. For now, here we are going to create a table with name codescrackerTable with 4 columns. Here is the screenshot demo of the entered data before creating table in MySQL database.

php mysqli beginner

After filling the detail as shown above, just click on the Go button. Here is window that will come out after clicking on the Go button to create MySQL database table.

php and mysqli for beginner

As you can see from the above window, you will be asked to enter the table details, that is columns name with its essential details.

Fill the essential field as shown here in the below screenshot after filling the details of the table that is recently created named codescrackerTable:

php mysqli code

Here is the details, you have to enter in all the 4 fields one by one.

Name Type Length/Values Default Attributes Index A_I
id INT 10 Primary (then click on OK) Tick on Checkbox
date TIMESTAMP CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
userid 40
usercomment 400

In case of column named id, when you select Primary from the dropdown list of Index, here is the window that will come out:

php mysql window after selecting primary

You have to click on the OK button.

In the above field A_I stands for Auto Increment.

After doing all the above things, just click on the Save button present at the right-bottom of the same window.

After saving the entered details or after clicking on the Save button, here is the screenshot of the window that will come out.

php mysqli table

Now you have a newly created blank table with 4 column in your codescracker database. To check it out, click on the codescrackerTable button present at the left pannel of the phpMyAdmin window. Here is the demo screenshot of the window that will present after clicking on the codescrackerTable table.

php mysqli operation

Now here is the example of PHP and MySQLi, that is used to store data or feed data into MySQL database using PHP.

<?php 
	$conn = mysqli_connect('localhost', 'root', '', 'codescracker');
	if(mysqli_connect_error())
	{
		echo "<p>Error occurred while connecting to the MySQL database.</p>";
		echo "<p>Kindly try again later.</p>";
		echo "<p>Thank You!</p>";
		echo "<p><i>by CodesCracker Admin</i></p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Example</title>
</head>
<body>
<?php
	$userid = "codescracker@gmail.com";
	$usercomment = "this tutorial is only for those, who are interesting to 
	develop a user-friendly and database-driven website using PHP and MySQL";
	$queryStr = "insert into codescrackerTable(userid, usercomment) values('$userid', '$usercomment')";
	$result = $conn->query($queryStr);
	if($result)
	{
		echo "<p>Data inserted successfully.</p>";
	}
	else 
	{
		echo "<p>Error occurred while inserting the data into the MySQL database.</p>";
		echo "<p>Please try again later.</p>";
		echo "<p>Exiting.....</p>";
	}
?>
</body>
</html>

Type the above PHP and MySQLi code or just do copy and pase the above PHP and MySQLi code and save the file in the directory C:\xampp\htdocs\ with name codescracker.php and open your web browser and type localhost/codescracker.php.

Here is the demo output produced by the above PHP and MySQLi example code:

php and mysqli tutorial

After running the above PHP and MySQLi code in your browser, your data will be inserted into the database named codescracker.

Here is the screenshot of the table named codescrackerTable that holds the data inserted recently by the above PHP and MySQLi program.

php mysqli feed data

Now from next tutorial, you will learn step by step PHP and MySQLi tutorial with its detail and examples from opening MySQL database connection to closing MySQL database connection using PHP with MySQLi.


« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test