very basic code which we use offten in our day to day development and sometime we stucked . i write those issue here with simple solution. You will get codes of html, css, javascript, jquery, json,ajax, node.js, vue.js, nuxt.js, php and Mysql
Saturday, January 30, 2016
Thursday, January 28, 2016
How to create table backup using native Php Script ( Using PHP/PDO )
Take database backup using PHP Script
------------------------
<?php
session_start();
$con = include("connection.php");
function backup_db($con){
/* Store All Table name in an Array */
$allTables = array();
$query = $con->query('SHOW TABLES');
$result=$query->fetchAll(PDO::FETCH_COLUMN);
$allTables=$result;
$return ='';
foreach($allTables as $table){
$result_tab = $con->query('SELECT * FROM '.$table);
$num_fields = $result_tab->columnCount();
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$create_query = $con->query('SHOW CREATE TABLE '.$table);
$row2 = $create_query->fetch(PDO::FETCH_NUM);
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
$eachRow =$result_tab->fetchAll(PDO::FETCH_NUM);
foreach($eachRow as $keys=>$row)
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++){
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j]))
{
$return.= '"'.$row[$j].'"' ;
}
else
{
$return.= '""';
}
if($j<($num_fields-1))
{
$return.= ',';
}
}
$return.= ");\n";
}
}
$return.="\n\n";
}
// Create Backup Folder
$folder = 'sql/';
if (!is_dir($folder))
mkdir($folder, 0777, true);
chmod($folder, 0777);
$date = date('m-d-Y-H-i-s', time());
$filename = $folder."db-backup-".$date.'.sql';
$_SESSION['backupFile'] = $filename; // you can found out this file path while importing
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
}
// Call the function
backup_db($con);
?>
session_start();
$con = include("connection.php");
function backup_db($con){
/* Store All Table name in an Array */
$allTables = array();
$query = $con->query('SHOW TABLES');
$result=$query->fetchAll(PDO::FETCH_COLUMN);
$allTables=$result;
$return ='';
foreach($allTables as $table){
$result_tab = $con->query('SELECT * FROM '.$table);
$num_fields = $result_tab->columnCount();
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$create_query = $con->query('SHOW CREATE TABLE '.$table);
$row2 = $create_query->fetch(PDO::FETCH_NUM);
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
$eachRow =$result_tab->fetchAll(PDO::FETCH_NUM);
foreach($eachRow as $keys=>$row)
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++){
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j]))
{
$return.= '"'.$row[$j].'"' ;
}
else
{
$return.= '""';
}
if($j<($num_fields-1))
{
$return.= ',';
}
}
$return.= ");\n";
}
}
$return.="\n\n";
}
// Create Backup Folder
$folder = 'sql/';
if (!is_dir($folder))
mkdir($folder, 0777, true);
chmod($folder, 0777);
$date = date('m-d-Y-H-i-s', time());
$filename = $folder."db-backup-".$date.'.sql';
$_SESSION['backupFile'] = $filename; // you can found out this file path while importing
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
}
// Call the function
backup_db($con);
?>
How to import sql backup using php script ( Using PHP/PDO ).
Make a Copy of Tables using PHP / PDO
session_start();
$con = include("config.php");
$filename = 'path_to_file/backup_db.sql';
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
$query = $con->prepare($templine); // Perform the query
$done = $query->execute();
$templine = ''; //free up memory
}
}
echo "Tables imported successfully";
Monday, January 4, 2016
How to connect mysqli with php using PDO?
$host = "localhost";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=$host;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
Saturday, December 19, 2015
how to create RSS Feed
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>';?>
<rss version="2.0">
<channel>
<title>BhojpuriTrain.com</title>
<description>Free Bhojpuri song download</description>
<link>http://bhojpuritrain.com</link>
<item>
<title>BhojpuriTrain.com</title>
<description>Watch PK2 Comedy Short Film directed by SRikanth Reddy PK2 is a hilarious Hindi short film spoof</description>
<link>http://bhojpuritrain.com/player.php?v=LHu3_CXEdJY</link>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
</item>
<item>
<title>A 2 year old cute kid Dugu answering the quote answer</title>
<description>Must watch how this cute boy answering</description>
<link>http://bhojpuritrain.com/player.php?v=od0YNytKPCo</link>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
</item>
<item>
<title>a very cute dance by village boy</title>
<description>must see how a little kid dancing</description>
<link>http://bhojpuritrain.com/player.php?v=didoFn8Erf0</link>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
</item>
</channel>
</rss>
Subscribe to:
Posts (Atom)