简体   繁体   中英

Multiple SQL query not executing in Cakephp

Here's the scenario, I am uploading a txt file of sql queries in it. And every successful upload, I need to execute its content as well.

Well, I cant seem to execute set of queries. Just single line query.

I have query.txt:

USE `wifi_analyzer`;

CREATE TABLE 'test' (
    'username' varchar(10) NOT NULL,
    'password' varchar(10) NOT NULL
    );

And this is how i execute it in my cakephp file.

$fullFile = $file_path.$file_name;
$readFile = fopen($file_path.$file_name, 'r');
$contents = fread($readFile,filesize($fullFile));
$this->User->query($contents);

The file can be read as well as its content. However, It displays an error:

SQL Error: 1064: 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 'CREATE TABLE 'test' (
    'username' varchar(10) NOT NULL,
    'password' varchar(10' at line 3 [CORE\cake\libs\model\datasources\dbo_source.php, line 673]

Well, if it is just a single line query like this:

Select * from sometable;

it doesn't display any error. But this single query:

CREATE TABLE 'test' (
'username' varchar(10) NOT NULL,
'password' varchar(10) NOT NULL
);

it does not execute as well.


UPDATE: Well i have read this: http://www.hardcode.nl/subcategory_4/article_558-execute-mysql-sql-dump-files-via-php-mysqli.htm

but dunno if this will help since i am doing it in cakephp

Dont use single quotes for table and field names. Execute below query it will work

CREATE TABLE `test` (
`username` varchar( 10 ) NOT NULL ,
`password` varchar( 10 ) NOT NULL
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM