PHP Select Data From MySQL

The SELECT statement is used to select data from one or more tables:

include (‘function/db.php’);
global $conn;

$id = “1”;

$query = “SELECT * FROM table WHERE id=’$id'”;
$result = $conn->query($query);
$row = $result->fetch_assoc();

$id = $row[‘id’];
$data1 = $row[‘col1’];
$data2 = $row[‘col2’];

        $data3 = $row[‘col3’];
        $data4 = $row[‘col4’];

And you can show your result anywhere on your web page like –

Name  = <h1><?php  echo $data1 ?></h1>

Address  = <p><?php  echo $data2 ?></p>

Password Change With Old Password

Change your Password With Your Original Password

 

if(isset($_POST[‘changepassword’]))
{

$cp = $_POST[‘currentpass’];
$np1 = $_POST[‘newpass1’];
$np2 = $_POST[‘newpass2’];
$id = $_POST[‘id’];

$chck = $conn->query(“select * from profile WHERE id=’$id'”);
$r = $chck->fetch_assoc();
$opass = $r[‘pass’];

if($np1 != $np2)
{
$_SESSION[‘passwordchange’] = “New Password Not Match!”;
header(‘Location: ‘ . $_SERVER[‘HTTP_REFERER’]);
}
else if($cp != $opass)
{
$_SESSION[‘passwordchange’] = “Your Current Password not Match!”;
header(‘Location: ‘ . $_SERVER[‘HTTP_REFERER’]);
}
else
{
$result = mysqli_query($conn ,”update profile set pass=’$np2′ WHERE  id=’$id'”);
$match = $conn->query(“select * from profile WHERE member_id=’$id'”);
$row = $match->fetch_assoc();
$npass = $row[‘pass’];
if($np1 == $npass)
{
$_SESSION[‘passwordchange’] = “Your Password Has Been Successfully Updated!”;
header(‘Location: ‘ . $_SERVER[‘HTTP_REFERER’]);
}
else
{
$_SESSION[‘passwordchange’] = “Something Went wrong!”;
header(‘Location: ‘ . $_SERVER[‘HTTP_REFERER’]);
}
}
header(‘Location: ‘ . $_SERVER[‘HTTP_REFERER’]);
}

Insert into DB using ajax without refreshing page

Create a new file call ajax.php and add js-script to an index file or add a new js file.

index.php

<form>

<input class=”form-control” type=”text” id=”name” />

<input class=”form-control” type=”text” id=”contact”  />

<button class=”btn btn-primary” type=”button”  onclick=”insert()”>Add Details</button>

</form>

// show the result

.js script

 

function insertm() {

var mnm = $(‘#name’).val();

var mcon = $(‘#contact’).val();

if(mnm == “” || mcon == “”)

{

alert(‘Fill all the data!’);

}

else{

if (window.XMLHttpRequest) {

// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

} else { // code for IE6, IE5

xmlhttp=new ActiveXObject(‘Microsoft.XMLHTTP’);

}

xmlhttp.onreadystatechange=function() {

if (xmlhttp.readyState==4 && xmlhttp.status==200) {

document.getElementById(‘gid’).innerHTML=xmlhttp.responseText;

}

}

xmlhttp.open(‘GET’,’ajax.php?n=’+mnm +’&c=’+mcon ,true);

xmlhttp.send();

}

$(‘#name’).val(“”);

$(‘#contact’).val(“”);

}

ajax.php

 

<?php

session_start();

include(‘function/db.php’);

global $conn;

$name = $_GET[‘n’];

$contact = $_GET[‘c’];

$stmt = $conn->prepare(“INSERT INTO profile(name,cont) VALUES (?, ?)”);

$stmt->bind_param(“ss”, $nm, $con);

//set parameters and execute

$nm = $name;

$con = $contact;

$stmt->execute();

$last_id = $conn->insert_id;

if($stmt)

{

echo “<span class=’sucsses’>Added Successfully with id</span> $last_id</span>”;

}

else

{

echo “Somthing Went Wrong!”;

}

$stmt->close();

?>

How To Get Difference between two dates

Get Difference between two dates

 

Date1 – dd-mm-yyyy
Date2 – dd-mm-yyyy

Break and get the part of the date, month and year and show the difference.

$today = date(“d-m-Y”);

$d1 = substr($d, 0,-8);
$d2 = substr($today, 0,-8);

$m1 = substr($d, 3,-5);
$m2 = substr($today, 3,-5);

$y1 = substr($d, -4);
$y2 = substr($today, -4);

$date1 = “$y1-$m1-$d1”;
$date2 = “$y2-$m2-$d2”;

$diff = abs(strtotime($date2) – strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff – $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff – $years * 365*60*60*24 – $months*30*60*60*24)/ (60*60*24));

And you can show your result anywhere on your web page like –

<?php if($days){echo $days . ” days,”;};if($months){echo $months .” months and”;};if($years){echo $years .” years”;};echo ” lefts.” ; ?>

This will be shown like –
dd days, mm months and yy years lefts.

Get the Only Date part from DateTime SQL

Get the only date from date time in database

 

Real Datetime shows like – yyyy-mm-dd hh:mm:ss

And Now you can break and get the part of the date, time as per your requirement.

$dd = substr($date, 8,2);

$mm = substr($date, 5,2);
$yyyy = substr($date, 0,4);
$newdate = “$dd-$mm-$yyyy”;

And you can show your result anywhere on your web page like –

newdate = dd-mm-yyyy

Empty a folder on the server

Make sure you have unique name directory

$directory = “images/directory”;
empty($directory);
function empty($directory)
{
$dirHandle = opendir($directory);
// Loop over all of the files in the folder
while ($file = readdir($dirHandle)) {
// If $file is NOT a directory remove it
if(!is_dir($file)) {
unlink (“$directory”.”$file”); // unlink() deletes the files
}
}
// Close the directory
closedir($dirHandle);

// you can skip this state in case you just want to remove directory only

if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
echo “directory empty successfully!”;
}

Upload a folder to the server and files inside too.

Make sure you have a directory on your server

 

function addfolder()
{
$directory = “images/”;
$count = 0;
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
foreach ($_FILES[‘files’][‘name’] as $i => $name) {
if (strlen($_FILES[‘files’][‘name’][$i]) > 1) {
if (move_uploaded_file($_FILES[‘files’][‘tmp_name’][$i], ‘$directory/’.$name)) {
$count++;
}
}
}
echo “Folder uploaded! Successfully!”;
}
}

See The Error Occur Last During Query

We are going to take error message into session

include(‘db.php’);
global $conn;

$run = mysqli_query($conn, “insert into table(column) values (‘testing’)”);

if($run){
$_SESSION[‘msg’] = “success!”;
}
else
{
$_SESSION[‘msg’] = mysqli_error($conn);
}

To Print Here is The Code –

if(isset($_SESSION[‘msg’])){
echo $_SESSION[‘msg’];
unset($_SESSION[‘msg’]);
}

//run only once

Result Will Shown Like – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘s Very Complicated’)’ at line 1
//In case you insert the string ” It’s Very Complicated” SQL break after ‘ and this treat as SQL Injection

 

To Avoid This Try Secure Insertion Method – PHP – Insert Data Into MySQL Using Parameters
Design a site like this with WordPress.com
Get started