Backing up a MySQL database using PHP - php

I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>

Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());

Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>

<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>

Related

Database doesn't load

I want to open a count in a php file in order to get the first register
<?php
include_once('dbConfig.php');
session_start();
$id_persona= $_SESSION["id"];
$query = "SELECT COUNT( * ) AS alias1 FROM `desayuno_ingreso` WHERE `desayuno_ingreso`.`id` =88757532";
echo $query;
$resultset = mysqli_query($conn,$query);
if(mysqli_num_rows($resultset) > 0){
$k=0;
while ($fila = mysqli_fetch_assoc($resultset)) {
$lat[$k]= $fila['alias1'];
$k=$k+1;
}
}
echo $lat[0];
?>
I tried in every single way I know (Every way worked for me before). However, it doesn't working right now.
I'm no sure what I'm doing wrong.
EIDTED
This is the dbConfig.php file:
<?php
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = 'mypassword';
$dbName = 'app';
$conn = mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
if(!$conn){
die("Database connection failed: " . mysqli_connect_error());
}
?>

php mysql search error Couldn't execute query

I have written a php script to fetch data but somehow it has stopped working and giving Couldn't execute query, I am unable to find the problem.
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$id = $_GET['id'];
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");
You have used 2 times database name here. Just use this
$db_username = 'uname';
$db_password = 'password';
$db_name = 'db_name';
$db_host = 'localhost';
$db_select =mysql_connect('localhost', 'root','');
$db_=mysql_select_db($db_name,$db_select);
$sql = "SELECT * FROM datatable WHERE id = $id";
$result = mysql_query($sql) or die("Couldn't execute");

Database selection (PHP)

How do I choose a tbl_uploads table in the database?
PHPMYADMÄ°N IMG
<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
?>
You don't need to choose any table just fire the Query on the database in which the table exists, and you're done. For example,
<?php
$dbhost = "";
$dbuser = "";
$dbpass = "";
$dbname = "";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
$query = "SELECT * FROM tbl_uploads;"
//executing the query and printing it's results
$results= mysql_query($query);
print_r($results);
?>
This will print the results of query in the variable $results.
Note :
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()
First, connect to your database (keinotis_iletisim). For example:
$dbhost = "localhost"; // or any other address
$dbuser = ""; // the user you created for the database in phpmyadmin
$dbpass = ""; // the password you created for the database in phpmyadmin
$dbname = "keinotis_iletisim";
Then, in a query you can mention a table, for example:
SELECT * FROM tbl_uploads;
Also see this link on W3Schools.
You can select your table using sql statement like this
$sql = "SELECT * FROM MyGuests";
$result = mysql_query($conn, $sql);
Now you can fetch the all records by using
while($row=mysql_fetch_array($result))
{
echo $row['column_name'];
}
This one will work for you
<?php
//the connction
$hostname_localhost = "localhost";
$database_localhost = "keinotis_iletisim";
$username_localhost = "root";
$password_localhost = "";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_record = "SELECT * FROM tbl_uploads";
$record = mysql_query($query_record, $localhost) or die(mysql_error());
$row_record = mysql_fetch_assoc($record);
$totalRows_record = mysql_num_rows($record);
//echo $row_record['tbl_uploads_table_colum'];
?>
<!--And if you want to display the list-->
<?php do { ?>
<?php echo $row_record['tbl_uploads_table_colum']; ?>
<?php } while ($row_record = mysql_fetch_assoc($record)); ?>

Import data from mdb file to mysql database can not upload data

I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");

Jqgrid can Edit but not Add

I have my grid running and I can EDIT data but I cannot ADD new rows of data. When I submit the add form nothing happens and I get nothing back on my PHPMyAdmin page to show that the grid is hit the database with the INSERT statement.
Here is my Edit code.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*****";
$dbname = "******";
// connect to the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . mysql_error());
mysqli_select_db($conn,$dbname) or die("Error conecting to db.");
//$id = "'".$_POST['id']."'";
$unit = "'".$_POST['unit_id']."'";
$div = "'".$_POST['div_id']."'";
$titleorg = "'".$_POST['title_org']."'";
$shortdesc = "'".$_POST['short_desc']."'";
$longdesc = "'".$_POST['long_desc']."'";
$unitdesc = "'".$_POST['unit_desc']."'";
$enabled = "'".$_POST['avail_ind']."'";
$active = "'Y'";
switch($_POST['oper'])
{
case "add":
$query = "INSERT INTO depunits (unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind,active_ind) values ($div,$unit,$titleorg,$shortdesc,$longdesc,$unitdesc,$enabled,$active)";
mysqli_query($conn,$query);
break;
case "edit":
$query = "UPDATE depunits SET unit_id=$unit,div_id=$div,title_org=$titleorg,short_desc=$shortdesc,long_desc=$longdesc,unit_desc=$unitdesc,avail_ind=$enabled where unit_id=$unit";
mysqli_query($conn,$query);
break;
}
?>

Categories