Laravel 4 无法运行整个 RAW 查询
Posted
技术标签:
【中文标题】Laravel 4 无法运行整个 RAW 查询【英文标题】:Laravel 4 cannot run whole RAW queries 【发布时间】:2013-07-11 21:58:27 【问题描述】:我想使用 laravel 的 DB 类来执行一个 mysql 查询,但是 Laravel 提供的功能都不起作用。
这些都不起作用:DB::statment() / DB::select() / DB::raw() / DB::update() / DB::select(DB::raw())
这是我要查询的代码:
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","admin@example.com","$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","mod@example.com","$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","help@example.com","$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","dude@example.com","$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","girl@example.com","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0");
我也查看并尝试了这两个主题的方法,但没有任何效果:
cannot-run-raw-query-in-laravel-4
laravel-4-how-to-run-a-raw-sql
使用 "DB::select( DB::raw($query) )" 或 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","admin@example.com","$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","mod@example.com","$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","help@example.com","$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","dude@example.com","$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","girl@example.com","$2y$08$UKjJzxDuYW7upqeLsm1VOOo2jUoqMaai0/1jFxvLDzC6eWjin3yOe","0000-00-00 00:00:00","0000-00-00","0","0","0","0","0","0"); ) (Bindings: array ( ))
【问题讨论】:
你能在使用 DB 类执行查询时显示它给出的错误吗? 添加了返回到主要问题的错误。当我尝试使用 phpmyadmin 执行查询时,它运行良好 【参考方案1】:这段代码呢? 我发现使用migrations & db seeding 非常有用——尤其是用于部署。
Schema::create('users', function($table)
$table->increments('id');
$table->string('u_username');
$table->string('u_email');
$table->string('password');
$table->datetime('u_regdate');
$table->date('u_birthday');
$table->integer('u_lastlogin');
$table->integer('u_logcout')->default(0);
$table->tinyinteger('u_level')->default(0);
$table->tinyinteger('u_language')->default(0);
$table->tinyinteger('u_status')->default(0);
$table->tinyinteger('u_gender')->default(0);
);
// Repeat this for other users as well
User::create([
'u_username' => 'admin',
'u_email' => 'admin@example.com',
'password' => Hash::make('users-password'),
'u_regdate' => date('Y-m-d H:i:s'),
'u_birthday' => '1980-01-01',
'u_lastlogin' => 0,
'u_logcout' => 0,
'u_level' => 9,
'u_language' => 0,
'u_status' => 0,
'u_gender' => 0,
]);
【讨论】:
【参考方案2】:像我在问题中提供的那样执行 SQL 代码可以使用
DB::unprepared( $code );
我希望这可以帮助遇到同样问题的人:D
Answer found while looking through laravel issues
【讨论】:
@OmerM。您如何在系统中合并 DB 类。您是创建模型还是直接在控制器中调用? @Volatil3 对于这个项目,我在 PHP 命令类中使用了 DB 类。我想创建一个工匠命令,我可以用它来导出和导入我的数据库。您可以在整个项目中的任何地方使用 DB 类。在您的控制器或模型中。【参考方案3】:从数据库中删除用户表,然后试试这个,让我知道它是否有效:
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.
【讨论】:
DB::query 在 laravel 4 中不存在,所以我使用 DB::insert 并且它有效。我想同时执行多个查询是不可能的,但是一个一个地执行它们是可行的 是的,这是正确的,您需要单独执行查询,感谢您告知 laravel 4 中不存在 DB::query。我将使用 insert() 更改答案 刚发现有一个执行原始mysql代码的函数: DB::unprepared( $code ); github.com/laravel/framework/pull/54以上是关于Laravel 4 无法运行整个 RAW 查询的主要内容,如果未能解决你的问题,请参考以下文章