Home >>MySQL Tutorial >PHP Mysqli Database Connectivity

PHP Mysqli Database Connectivity

Mysqli PHP Connectivity

How to connect PHP through mysqli. Create a registration form with 4 four fields name,email id, mobile number and address. Fill all the fields click on save data submit button to save your data inside "users" table using mysqli prepared statements insert. To display all the values from "users" table click on disp data button it will display the values using mysqli prepared statements select.

Create Database and Table

//create a database
CREATE DATABASE `demo`;

USE demo;

//create a table
CREATE TABLE `demo`.`users` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`name` CHAR( 50 ) NOT NULL ,

`email` VARCHAR( 50 ) NOT NULL ,

`mobile` BIGINT NOT NULL ,

`address` TEXT NOT NULL ,

UNIQUE (
`email`

)
) ENGINE = MYISAM ;

PHP Script(PHP mysqli connection )

<?php

//mysqli connectivity
$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!!");

extract($_POST);

if(isset($save))
{

	$query="insert into users values('','$n','$e','$m','$add')";

	if($connect->query($query))

	{

	echo 'congrates!! data saved';

	}

	else

	{

	echo 'data not saved '.$connect->error;

	}

}

//display data
if(isset($disp))
{

	$query="select * from users";

	$result=$connect->query($query);

	echo "<table border=1>";

	echo "<tr><th>Name</th><th>Email</th><th>Mobile<
/th><th>Address</th></tr>";

	while($row=$result->fetch_array())

		{

		echo "<tr>";

		echo "<td>".$row['name']."</td>";

		echo "<td>".$row['email']."</td>";
		echo "<td>".$row['mobile']."</td>";

		echo "<td>".$row['address']."</td>";

		echo "</tr>";

		}

	echo "</table>";	

}

?>

HTML Form

<style>

input,textarea{width:200px}

input[type=submit]{width:100px}

</style>

<form method="post">

<table width="218" border="0">

  <tr>

    <td width="208"><input type="text" name="n" placeholder="Enter your name"/></td>

  </tr>

  <tr>

    <td><input type="text" name="e" placeholder="Enter your email"/></td>

  </tr>

  <tr>

    <td><input type="text" name="m" placeholder="Enter your mobile"/></td>

  </tr>

  <tr>

    <td><textarea name="add" placeholder="Enter your address"></textarea></td>

  </tr>

  <tr>

    <td>

	<input type="submit" name="save" value="Save Data"/>

	<input type="submit" name="disp" value="Display Data"/>

	</td>

  </tr>

</table>

</form>

mysqliconnectivity_phptpoint


No Sidebar ads