Jump to content

Recommended Posts

Can't find error

 


$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$tblName = 'name of database' //ie: clients

$db = new mysqli($dbhost, $dbuser, $dbpass);

if($db->connect_errno > 0){
   die('Unable to connect to database [' . $db->connect_error . ']');
}

$today = date('Y-m-d');
$backupFile = 'backup/esafeclients-$today.sql';
$sql = "SELECT * INTO OUTFILE '$backupFile' FROM $tblName";

if(!$result = $db->query($sql)){
   die('There was an error running the query [' . $db->error . ']');
}

 

I am getting this error

 

There was an error running the query [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 '' at line 1]

Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/
Share on other sites

echo the query along with it and you'll see it.

 

The code you've posted shouldn't produce that error.

 

Edit: Oh yes it will. You have two possible issues.

Read this: http://dev.mysql.com/doc/refman/5.0/en/select.html

Edited by Jessica
Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406166
Share on other sites

Well he understood it.

 

The query is malformed. The right way is

"SELECT * FROM $tblName INTO OUTFILE '$backupFile'"

 

There is a big difference.

 

 

It appears he does also need to select a DB according to this code. It's possible he left that part out when copying it, because I don't think MySQL would return the error it did if that were the case.

Edited by Jessica
Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406185
Share on other sites

I do, but no one will listen to me.

 

Just a hint Jessica I am male.... :happy-04:

 

Yeah forgetting the database part was my error on posting.

 

Now I have a new problem. Using same username and password for my other scripts but for some reason it is saying

There was an error running the query [Access denied for user 'username'@'localhost' (using password: YES)]

 

This is now I hope the final piece of code:

 

$db = new mysqli($dbhost, $dbuser, $dbpass, $tblname);

if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}

$today = date('Y-m-d');
$backupFile = './backup/esafeclients-'.$today.'.sql';
$sql = "SELECT * FROM $tblname INTO OUTFILE '$backupFile'";

if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}

 

I have tried to echo $sql with no result.

 

Should I be using the username and pass that is attached to this database or should I be using my hosting account uname and pass.

 

PS: If you think I should just give up and do it manually through phpmyadmin just say so I wouldn't mind right about now....lol

Edited by SkyRanger
Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406202
Share on other sites

The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax.

 

If this a shared hosting, it's unlikely you have or will be given the needed privileges to run this query.

Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406205
Share on other sites

 

 

If this a shared hosting, it's unlikely you have or will be given the needed privileges to run this query.

 

Thanks for the link, mostly because it has this at the top:

"The SELECT syntax description (see Section 13.2.8, “SELECT Syntax”) shows the INTO clause near the end of the statement. It is also possible to use INTO immediately following the select_expr list."

 

So I go back to my very first thought which is one of those variables is empty, producing the ''. Probably the one surrounded by them in the first place. 

Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406206
Share on other sites

Sorry guys, had to run out for a bit. $tblName is actually the Database name.

 

Sorry Jessica, never added that to this post. Didn't think it was relevant for trying to fix the code I needed. no actually it is suppose to be esafe_clients@

 

I know the output comes out at

select * from esafe_clients into outfile esafeclients-2013-01-16..sql

 

 

Just got the user privilages for this database. could this be the problem

 

 

esafe_clients

 

Alter, Create, create routine, create temporary tables, create view, delete, drop, execute, index, insert, lock tables, references, select, show view ,trigger, update

Edited by SkyRanger
Link to comment
https://forums.phpfreaks.com/topic/273244-database-backup/#findComment-1406259
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.