Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 cannot run whole RAW queries

I would like to use the DB class of laravel to execute a mysql query but none of the functions provided by Laravel are working.

None of those is working: DB::statment() / DB::select() / DB::raw() / DB::update() / DB::select(DB::raw())

Here is the code I would like to query:

DROP TABLE users;

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `u_birthday` date NOT NULL DEFAULT '0000-00-00',
  `u_lastlogin` int(11) NOT NULL,
  `u_logcout` int(11) NOT NULL DEFAULT '0',
  `u_level` tinyint(1) NOT NULL DEFAULT '0',
  `u_language` tinyint(1) NOT NULL DEFAULT '0',
  `u_status` tinyint(1) NOT NULL DEFAULT '0',
  `u_gender` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO users VALUES("1","admin","[email protected]","$2y$08$9sBJh7iyF9Yr6xvsieNmBOsotGPBkZFydVjb/Yk5Fzh4IGBVo7Je6","0000-00-00 00:00:00","0000-00-00","0","0","9","0","0","0");
INSERT INTO users VALUES("2","moderator","[email protected]","$2y$08$15/tIKPM/8gATSzkmey5tuAA/PL4lJeFq7liTETyz0h1dkOotwp3G","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");
INSERT INTO users VALUES("3","helper","[email protected]","$2y$08$nTtZU9/UBeRLbYJRO/XwCe2D/B57ofx2bCN8vGEiHkqO.bPra0WT6","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");
INSERT INTO users VALUES("4","dude12","[email protected]","$2y$08$y0JweKtWxJFRF7Ko8q0zkODY.EWEKJ.CR1dDco6aCJh8ssKdzQ6RC","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");
INSERT INTO users VALUES("5","girl1","[email protected]","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");

I have looked and tried methods from those two topics too but nothing is working:

cannot-run-raw-query-in-laravel-4

laravel-4-how-to-run-a-raw-sql

Error returned when executing with "DB::select( DB::raw($query) )" or DB::statement :

SQLSTATE[42000]: Syntax error or access violation: 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 `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_use' at line 3 (SQL: DROP TABLE users; CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `u_birthday` date NOT NULL DEFAULT '0000-00-00', `u_lastlogin` int(11) NOT NULL, `u_logcout` int(11) NOT NULL DEFAULT '0', `u_level` tinyint(1) NOT NULL DEFAULT '0', `u_language` tinyint(1) NOT NULL DEFAULT '0', `u_status` tinyint(1) NOT NULL DEFAULT '0', `u_gender` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO users VALUES("1","admin","[email protected]","$2y$08$9sBJh7iyF9Yr6xvsieNmBOsotGPBkZFydVjb/Yk5Fzh4IGBVo7Je6","0000-00-00 00:00:00","0000-00-00","0","0","9","0","0","0"); INSERT INTO users VALUES("2","moderator","[email protected]","$2y$08$15/tIKPM/8gATSzkmey5tuAA/PL4lJeFq7liTETyz0h1dkOotwp3G","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("3","helper","[email protected]","$2y$08$nTtZU9/UBeRLbYJRO/XwCe2D/B57ofx2bCN8vGEiHkqO.bPra0WT6","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("4","dude12","[email protected]","$2y$08$y0JweKtWxJFRF7Ko8q0zkODY.EWEKJ.CR1dDco6aCJh8ssKdzQ6RC","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); INSERT INTO users VALUES("5","girl1","[email protected]","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); ) (Bindings: array ( ))
like image 425
Omer M. Avatar asked Jul 13 '13 13:07

Omer M.


2 Answers

Executing SQL Code just like the one I provided in the question can be executed using

DB::unprepared( $code );

I hope this helps the people with the same issue i had :D

Answer found while looking through laravel issues

like image 162
Omer M. Avatar answered Oct 04 '22 04:10

Omer M.


Drop the users table from database then try this and let me know whether it works or not:

DB::insert('CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `u_username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `u_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `u_regdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `u_birthday` date NOT NULL DEFAULT '0000-00-00',
  `u_lastlogin` int(11) NOT NULL,
  `u_logcout` int(11) NOT NULL DEFAULT '0',
  `u_level` tinyint(1) NOT NULL DEFAULT '0',
  `u_language` tinyint(1) NOT NULL DEFAULT '0',
  `u_status` tinyint(1) NOT NULL DEFAULT '0',
  `u_gender` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');

//and so on for all the other queries.
like image 28
saran banerjee Avatar answered Oct 04 '22 06:10

saran banerjee