简体   繁体   English

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

[英]Syntax error or access violation: 1064 You have an error in your SQL syntax in alter table

I am trying to alter the auto_increment by laravel stament function and it is giving the error. 我试图通过laravel stament函数改变auto_increment并且它给出了错误。

The complete error message displaying is SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 显示完整的错误消息是SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法中有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' 检查与您的MariaDB服务器版本对应的手册,以便在'?'附近使用正确的语法 at line 1 (SQL: alter table bills AUTO_INCREMENT=9) 在第1行(SQL:alter table 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>";
}

Variable placeholders are not allowed everywhere: 各地都不允许使用可变占位符

Within the statement, ? 在声明中,? characters can be used as parameter markers to indicate where data values are to be bound to the query later when you execute it. 可以将字符用作参数标记,以指示稍后在执行时将数据值绑定到查询的位置。

alter table requires a constant here, eg you could not use something like alter table bills AUTO_INCREMENT = (select max(id) from bills) . alter table在这里需要一个常量,例如你不能使用alter table bills AUTO_INCREMENT = (select max(id) from bills)这样的东西alter table bills AUTO_INCREMENT = (select max(id) from bills) Generally, everywhere you could do this, you are allowed to use placeholders (with some exceptions like limit , but those are then mentioned in the documentation ). 通常,在任何地方都可以执行此操作,您可以使用占位符(有一些例外情况,例如limit ,但在文档中会提到这些)。

So in this case you have to use the value directly: 所以在这种情况下你必须直接使用这个值:

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

Variable Placeholder is not allowed here. 此处不允许使用可变占位符。 So. 所以。 I have done by this way, 我这样做了,

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

You can write the laravel query in two diffrent way you can tokenize variable 您可以用两种不同的方式编写laravel查询,以便对变量进行标记

1) First method 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) Second method 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>";
}

You can also write query below but your query will be wide open for sql injection. 您也可以在下面编写查询,但您的查询将为sql注入打开。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误 - SQL error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax 奇怪的未解决的PDO错误:语法错误或访问冲突:1064 SQL语法有错误; - Strange unsolved PDO error: Syntax error or access violation: 1064 You have an error in your SQL syntax; 错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; - Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误; 制作迁移表时出错 - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; Error making a migration table &#39;PDOException&#39; 语法错误或访问冲突:1064 您的 SQL 语法有错误; 检查 - 'PDOException' Syntax error or access violation: 1064 You have an error in your SQL syntax; check query failedSQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法错误 - query failedSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax 未捕获的 PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误 - Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax QLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误 - LARAVEL - QLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax - LARAVEL SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误[Php PDO] - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax [Php PDO] 得到语法错误或访问冲突:1064您的SQL语法有错误; 在流明5.6 - got an Syntax error or access violation: 1064 You have an error in your SQL syntax; on lumen 5.6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM