语法错误或访问冲突:1064您在alter table中的SQL语法错误

时间:2019-05-30 07:42:04

标签: php mysql laravel

我正在尝试通过laravel stament函数更改auto_increment,它给出了错误消息。

显示的完整错误消息为SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参见表11。检查与您的MariaDB服务器版本相对应的手册,以获取在'?'附近使用的正确语法。在第1行(SQL:更改表bills AUTO_INCREMENT = 9)

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=?',array('invoice_no'=>$invoice_no));
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

3 个答案:

答案 0 :(得分:0)

Variable placeholders到处都不允许:

  

在语句中,?字符可用作参数标记,以指示稍后在执行查询时将数据值绑定到查询的位置。

alter table在此处需要一个常量,例如您不能使用类似alter table bills AUTO_INCREMENT = (select max(id) from bills)的名称。通常,在任何可以执行此操作的地方,都可以使用占位符(limit等例外,但documentation中会提及占位符)。

因此,在这种情况下,您必须直接使用该值:

DB::statement('alter table `bills` AUTO_INCREMENT = '.$invoice_no)

答案 1 :(得分:0)

此处不允许使用可变占位符。 所以。我已经通过这种方式完成了

DB::statement('alter table `bills` AUTO_INCREMENT = '.$invoice_no);

答案 2 :(得分:0)

您可以用两种不同的方式编写laravel查询,也可以标记变量

1)第一种方法

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=:invoice_no',array('invoice_no'=>$invoice_no));
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

2)第二种方法

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=?',[$invoice_no]);
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

您还可以在下面编写查询,但是您的查询将对SQL注入大开眼界。

DB::statement('alter table账单AUTO_INCREMENT = '.$invoice_no);