错误:SQLSTATE [42000]:使用cakePHP语法错误或访问冲突

时间:2022-09-21 08:56:10

I am using the framework cakePHP for my application. I programmed it on localhost with xampp and try to upload it on my website now. It worked without any problems on localhost. Now there is only this one page, which does not work on the new server. The other sites (which use the database connection too) work alright.

我正在使用框架cakePHP作为我的应用程序。我使用xampp在localhost上编程并尝试将其上传到我的网站上。它在localhost上没有任何问题。现在只有这一页,它在新服务器上不起作用。其他站点(也使用数据库连接)工作正常。

For this one site the following message appears:

对于这一个站点,将显示以下消息:

Error: 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 'add' at line 1

错误:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“添加”附近使用正确的语法

SQL Query: add

SQL查询:添加

The function add() looks like this.

add()函数看起来像这样。

public function add() { 
      //$this->create();
      $word_id = $this->Word->getWord_id();
      $save = $this->save(array('word_id' => $word_id, 'text' => $this->getText($word_id), 'mistake' => 0));
      return $save['Game']['id']; 
   }

On localhost I used MySQL-Client-Version: mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $ and PHP Version 5.3.8.

在localhost上我使用了MySQL-Client-Version:mysqlnd 5.0.8-dev - 20102224 - $ Revision:310735 $和PHP Version 5.3.8。

On the server I use MySQL-Client-Version: 5.1.62 and PHP Version 5.3.17.

在服务器上,我使用MySQL-Client-Version:5.1.62和PHP Version 5.3.17。

Thank you very much for helping!

非常感谢您的帮助!

Edit: The model 'Game':

编辑:模型'游戏':

class Game extends AppModel {
   public $name = 'Game';
   public $belongsTo = 'Word';
   public $searchedWord = '';

   public function addGame() { // Create new game
      $word_id = $this->Word->getWord_id();
      $save = $this->save(array('word_id' => $word_id, 'text' => $this->getText($word_id), 'mistake' => 0));
      return $save['Game']['id']; // Build the hangman
    }
}

When I debug $this->Game, the output is:

当我调试$ this-> Game时,输出是:

object(AppModel) {
        useDbConfig => 'default'
        useTable => 'games'
        id => null
        data => array()
        schemaName => null
        table => 'games'
        primaryKey => 'id'
        validate => array()
        validationErrors => array()
        validationDomain => null
        name => 'Game'
        alias => 'Game'
        tableToModel => array(
            'games' => 'Game'
        )
        cacheQueries => false
        belongsTo => array()
        hasOne => array()
        hasMany => array()
        hasAndBelongsToMany => array()
        actsAs => null
        Behaviors => object(BehaviorCollection) {
            modelName => 'Game'
            defaultPriority => (int) 10
        }
        whitelist => array()
        cacheSources => true
        findQueryType => null
        recursive => (int) 1
        order => null
        virtualFields => array()
        __backAssociation => array()
        __backInnerAssociation => array()
        __backOriginalAssociation => array()
        __backContainableAssociation => array()
        findMethods => array(
            'all' => true,
            'first' => true,
            'count' => true,
            'neighbors' => true,
            'list' => true,
            'threaded' => true
        )
}

3 个解决方案

#1


3  

usually, if this error happens, you don't have the model instance, but an app model instance you work on. the app model instance doesnt have the add() method and directly queries the db with add().

通常,如果发生此错误,则您没有模型实例,而是您正在处理的应用模型实例。 app模型实例没有add()方法,并使用add()直接查询db。

so make sure your model is properly included. since you didnt show us the code how you call the method (and how you make the model available to the controller) I cannot offer any concrete advice, though.

所以请确保您的模型已正确包含在内。既然你没有向我们展示你如何调用方法的代码(以及如何使模型可用于控制器),我不能提供任何具体的建议。

if you manually include it:

如果您手动包含它:

$this->ModelName = ClassRegistry::init('ModelName');

#2


0  

add is a reserved word in MySQL and you're probably using it in a SQL query without "escape".

add是MySQL中的保留字,你可能在没有“escape”的SQL查询中使用它。

Check if you have any field named add in your database.

检查数据库中是否有任何名为add的字段。

#3


0  

I just had this error and I felt pretty stupid. I'm sure this has been solved a long time ago, but in case anyone else comes across it...

我刚刚遇到这个错误,我觉得非常愚蠢。我相信很久以前就已经解决了这个问题,但是如果有其他人遇到它......

Using your example I'll show basically what I also stupidly did in my Controller and how it caused the same type of error you had:

使用你的例子,我将基本上展示我在控制器中愚蠢地做了什么以及它如何导致你所遇到的同样类型的错误:

 public function index($gameid = null, $letter = null) {
      if ($gameid == null) {
           // New game
           $gameid = $this->Game->addGame();
      }
 }

Since you already have the instance (controller class) and you're not calling the Model method of addGame here, but the Controller's method, you simply remove the Game-> from your one-line command.

既然你已经拥有了实例(控制器类)并且你没有在这里调用addGame的Model方法,而是调用Controller的方法,那么你只需从你的单行命令中删除Game->。

 $gameid = $this->addGame();

Simple and easy oversight. That said, if you moved the addGame method to your Model class, it probably would have worked as expected. :)

简单易行的监督。也就是说,如果您将addGame方法移动到Model类,它可能会按预期工作。 :)

#1


3  

usually, if this error happens, you don't have the model instance, but an app model instance you work on. the app model instance doesnt have the add() method and directly queries the db with add().

通常,如果发生此错误,则您没有模型实例,而是您正在处理的应用模型实例。 app模型实例没有add()方法,并使用add()直接查询db。

so make sure your model is properly included. since you didnt show us the code how you call the method (and how you make the model available to the controller) I cannot offer any concrete advice, though.

所以请确保您的模型已正确包含在内。既然你没有向我们展示你如何调用方法的代码(以及如何使模型可用于控制器),我不能提供任何具体的建议。

if you manually include it:

如果您手动包含它:

$this->ModelName = ClassRegistry::init('ModelName');

#2


0  

add is a reserved word in MySQL and you're probably using it in a SQL query without "escape".

add是MySQL中的保留字,你可能在没有“escape”的SQL查询中使用它。

Check if you have any field named add in your database.

检查数据库中是否有任何名为add的字段。

#3


0  

I just had this error and I felt pretty stupid. I'm sure this has been solved a long time ago, but in case anyone else comes across it...

我刚刚遇到这个错误,我觉得非常愚蠢。我相信很久以前就已经解决了这个问题,但是如果有其他人遇到它......

Using your example I'll show basically what I also stupidly did in my Controller and how it caused the same type of error you had:

使用你的例子,我将基本上展示我在控制器中愚蠢地做了什么以及它如何导致你所遇到的同样类型的错误:

 public function index($gameid = null, $letter = null) {
      if ($gameid == null) {
           // New game
           $gameid = $this->Game->addGame();
      }
 }

Since you already have the instance (controller class) and you're not calling the Model method of addGame here, but the Controller's method, you simply remove the Game-> from your one-line command.

既然你已经拥有了实例(控制器类)并且你没有在这里调用addGame的Model方法,而是调用Controller的方法,那么你只需从你的单行命令中删除Game->。

 $gameid = $this->addGame();

Simple and easy oversight. That said, if you moved the addGame method to your Model class, it probably would have worked as expected. :)

简单易行的监督。也就是说,如果您将addGame方法移动到Model类,它可能会按预期工作。 :)