PHP Tutorials
- What is MySqli
- mysql query
- mysql query example
- InnoDB
- mysql column Type
- CRUD Example
- Connection Using Function
- mysql keys
- SELECT
- WHERE
- UPDATE
- Count no of Rows
- ALIAS
- AND, AND & OR
- BETWEEN
- COMPARISON OPERATOR
- DELETE
- DELETE LIMIT
- DISTINCT
- EXISTS
- FROM
- GROUP BY
- HAVING
- IN
- INTERSECT
- IS NULL & IS NOT NULL
- LIKE
- NOT
- ORDER BY
- SELECT LIMIT
- SUBQUERY
- TRUNCATE
- UNION && UNION ALL
- Concat & Group_Concat
- mysql Function
- Mysql Insert Id
- MySql Aggregate Function
- Mysql Join
- JOIN in MySql
- Trigger
- Procedure
- Transaction
- views
- Index
- SQL Injection
- Normalization
- Query Bind
- Interview Questions
Important Link
Database Connection Using Function.
File name : db_con.php
<?php
session_start();
//error_reporting(E_ERROR);
function db_connect() {
// Define connection as a static variable, to avoid connecting more than once
static $conn;
// Try and connect to the database, if a connection has not been established yet
if(!isset($conn)) {
$host = "localhost";
$user = "root";
$password = "";
$db_name = "db_test";
$conn = mysqli_connect($host,$user,$password,$db_name);
}
// If connection was not successful, handle the error
if($conn === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return mysqli_connect_error();
}
else
{
//echo "success connection";
}
return $conn;
}
function db_query($query) {
// Connect to the database
$connection = db_connect();
// Query the database
//$result = mysqli_query($connection,$query) or ($error = mysqli_error());
$result = mysqli_query($connection,$query);
return $result;
}
function db_error() {
$connection = db_connect();
return mysqli_error($connection);
}
/*
$connection = db_connect();
// Query the database
$qry = "select * from user_info";
$result = mysqli_query($connection,$qry);
while ($row = mysqli_fetch_array($result))
{
echo $row['name'];
}
*/
/* **************************************** */
/*
function db_query($query) {
// Connect to the database
$connection = db_connect();
// Query the database
$result = mysqli_query($connection,$query);
return $result;
}
*/
// An insertion query. $result will be `true` if successful
//$result = db_query("INSERT INTO `users` (`name`,`email`) VALUES ('John Doe','john.doe@gmail.com')");
/*
$result = db_query("select * from user_info");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
// We successfully inserted a row into the database
while ($row = mysqli_fetch_array($result))
{
echo $row['name'];
}
}
*/
/*
//$result = db_query("SELECT `name`,`email` FROM `users` WHERE id=5");
$result = db_query("select * from user_info");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
} else {
// Fetch all the rows in an array
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
print_r($rows);
}
return $rows;
}
*/
/* **************************************** */
/* **************************************** */
/*
function db_error() {
$connection = db_connect();
return mysqli_error($connection);
}
*/
/* **************************************** */
/* **************************************** */
/*
function db_quote($value) {
$connection = db_connect();
return "'" . mysqli_real_escape_string($connection,$value) . "'";
}
// Quote and escape form submitted values
$name = db_quote($_POST['username']);
$email = db_quote($_POST['email']);
// Insert the values into the database
$result = db_query("INSERT INTO `users` (`name`,`email`) VALUES (" . $name . "," . $email . ")");
*/
/* **************************************** */
?>
File name : function_details.php
<?php
function login_check($username, $password)
{
$result = db_query("select * from login_info where user_name='$username' and user_password='$password'");
$count = mysqli_num_rows($result);
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
header("Location:login_user.php");
return false;
}
else {
if($count == 1)
{
$_SESSION['user_name'] = $username;
header("Location:login_success.php");
exit;
}
}
}
function user_signup($fname,$lname,$address,$gender,$dob,$mobile,$email)
{
$result = db_query("INSERT INTO signup_user (first_name,last_name,address,gender,dob,mobile,email) VALUES ('$fname','$lname','$address','$gender','$dob','$mobile','$email')");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
echo '<script language="javascript">';
echo 'alert("Record inserted successfully")';
echo '</script>';
//echo ("<SCRIPT LANGUAGE='JavaScript'> window.location='../about_us.php'; </SCRIPT>");
}
}
//show_data($fname,$lname,$address,$gender,$dob,$mobile,$email);
/*
function show_data()
{
$result = db_query("select * from signup_user");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
while ($row = mysqli_fetch_array($result))
{
echo $row['first_name'];
echo $row['last_name'];
echo $row['address'];
echo $row['gender'];
echo $row['dob'];
echo $row['mobile'];
echo $row['email'];
}
}
return $result;
}
*/
function del_user_info($id)
{
$result = db_query("delete from signup_user where id = '$id'");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
echo '<script language="javascript">';
echo 'alert("Record Deleted successfully")';
echo '</script>';
echo ("<SCRIPT LANGUAGE='JavaScript'> window.location='show_user.php'; </SCRIPT>");
}
return $result;
}
function user_update($fname,$lname,$address,$gender,$dob,$mobile,$email,$id)
{
$result = db_query("update signup_user set first_name='$fname',last_name='$lname',address='$address',gender='$gender',dob='$dob',mobile='$mobile',email='$email' where id='$id'");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
echo '<script language="javascript">';
echo 'alert("Record Updated successfully")';
echo '</script>';
echo ("<SCRIPT LANGUAGE='JavaScript'> window.location='show_user.php'; </SCRIPT>");
}
}
?>
File name : index.php
<html>
<head>
<title></title>
</head>
<body>
<a href="signup_user.php">Sign UP</a>
<a href="login_user.php">Login</a><br/>
<a href="show_user.php">Show User</a>
</body>
</html>
File name : login_user.php
<?php
include 'db_con.php';
include 'function_details.php';
if(isset($_POST['submit']))
{
$username = $_POST['user_name'];
$password = $_POST['user_password'];
login_check($username,$password);
}
?>
<html>
<head>
<title></title>
</head>
<body>
<h2>Login User</h2>
<form action="" name="frm" method="post">
<input type="text" name="user_name"/><br/>
<input type="password" name="user_password"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
File name : signup_user.php
<html>
<head>
<title>User Signup</title>
</head>
<body>
<?php
include 'db_con.php';
include 'function_details.php';
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
user_signup($fname,$lname,$address,$gender,$dob,$mobile,$email);
}
?>
<form action="" method="post" name="frm">
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Last Name</label></td>
<td><input type="text" name="lname"/></td>
</tr>
<tr>
<td><label>Address</label></td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td><label>Gender</label></td>
<td><input type="radio" name="gender" value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
</td>
</tr>
<tr>
<td><label>Date OF Birth</label></td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td><label>Mobile</label></td>
<td><input type="text" name="mobile"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
File name : show_user.php
<?php
include 'db_con.php';
include 'function_details.php';
?>
<html>
<head>
<title>User Details</title>
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Gender</th>
<th>Dob</th>
<th>Mobile</th>
<th>Email</th>
<th>Action</th>
</tr></thead>
<?php
//show_data($fname,$lname,$address,$gender,$dob,$mobile,$email);
$result = db_query("select * from signup_user");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td><?php echo $row['address'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['mobile'];?></td>
<td><?php echo $row['email'];?></td>
<td><a href="edit_user_info.php?id=<?php echo $row['id'];?>">Edit</a></td>
<td><a href="del_user_info.php?id=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php
} }
?>
</table>
</body>
</html>
File name : delete_user_info.php
<?php
include 'db_con.php';
include 'function_details.php';
$id = $_GET['id'];
del_user_info($id);
?>
File name : edit_user_info.php
<?php
include 'db_con.php';
$id = $_GET['id'];
include 'function_details.php';
if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
user_update($fname,$lname,$address,$gender,$dob,$mobile,$email,$id);
}
?>
<html>
<head>
<title>User Signup</title>
</head>
<body>
<form action="" method="post" name="frm">
<?php
//show_data($fname,$lname,$address,$gender,$dob,$mobile,$email);
$result = db_query("select * from signup_user");
if($result === false) {
// Handle failure - log the error, notify administrator, etc.
$error = db_error();
// Send the error to an administrator, log to a file, etc.
return false;
}
else {
while ($row = mysqli_fetch_array($result))
{
?>
<table>
<tr>
<td><label>First Name</label></td>
<td><input type="text" name="fname" value="<?php echo $row['first_name'];?>"/></td>
</tr>
<tr>
<td><label>Last Name</label></td>
<td><input type="text" name="lname" value="<?php echo $row['last_name'];?>"/></td>
</tr>
<tr>
<td><label>Address</label></td>
<td><input type="text" name="address" value="<?php echo $row['address'];?>"/></td>
</tr>
<tr>
<td><label>Gender</label></td>
<td><input type="text" name="gender" value="<?php echo $row['gender'];?>"/></td>
</td>
</tr>
<tr>
<td><label>Date OF Birth</label></td>
<td><input type="text" name="dob" value="<?php echo $row['dob'];?>"/></td>
</tr>
<tr>
<td><label>Mobile</label></td>
<td><input type="text" name="mobile" value="<?php echo $row['mobile'];?>"/></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>"/></td>
</tr>
<tr>
<td><label></label></td>
<td><input type="submit" name="submit" value="Update"/></td>
</tr>
</table>
<?php
} }
?>
</form>
</body>
</html>
File name : upload_files.php
<?php
include 'db_con.php';
include 'function_details.php';
if(isset($_FILES['img_manager']))
{
$errors= array();
$file_name = $_FILES['img_manager']['name'];
$file_size =$_FILES['img_manager']['size'];
$file_tmp =$_FILES['img_manager']['tmp_name'];
$file_type=$_FILES['img_manager']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['img_manager']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" name="frm" action="" enctype="multipart/form-data">
<label>Image Name</label><input type="file" name="img_manager"/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
File name : index.php