dsovc00684 2014-03-27 11:57
浏览 135
已采纳

PHP和Mysql多次更新 - allowMultiQueries等效

I have just started working with Laravel and I was wondering if there was an equivalent of the allowMultiQueries connection string parameter that I would use in java.

Is there way to do multiple updates in the same call or do they have to be done in succession?

DB::update('DROP TABLE IF EXISTS tablename; CREATE TABLE tablename(...);');

instead of

DB::update('DROP TABLE IF EXISTS tablename;'); 
DB::update('CREATE TABLE tablename(...);');
  • 写回答

1条回答 默认 最新

  • drygauost253590142 2014-03-27 19:25
    关注

    You can do it fluently the Laravel way using Schema Builder like so

    use Illuminate\Database\Schema\Blueprint;
    
    ...
    
    if (Schema::hasTable('tablename')) {
        Schema::drop('tablename');
    }
    Schema::create('tablename', function(Blueprint $table) {
        $table->increments('id');
        $table->string('name', 64);
        $table->timestamps();
        $table->index('name');
    });
    

    But if you for some reason want to execute raw queries you can use unprepared() method in Query Builder

    DB::unprepared('DROP TABLE IF EXISTS tablename; CREATE TABLE tablename (id INT ...)');    
    

    but at least make sure you're using the proper syntax

    DROP TABLE IF EXISTS tablename
               ^^^^^^^^^
    

    instead of

    DROP TABLE tablename IF EXISTS
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部