Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM error - 1064 You have an error in your SQL syntax .... near '."id" AS "t0_c0"
  • First time using ORM. I just want a 'hello world' to demo finding some records from MySQL with FuelPHP 1.4. 

    My fuel_test database table called 'users' looks like this:

    id (int)  | name (varchar) | description (varchar) | company (varchar)

    My database config:

    'default' => array(
    'connection'     => array(
    'dsn'            => 'mysql:host=localhost;dbname=fuel_test',
    'username'       => 'fuel',
    'password'       => 'fuel',
    ),
    ),

    My ORM model:

    class Model_User extends Orm\Model
    {

      protected static $_table_name = 'users';
      protected static $_primary_key = array('id');

      protected static $_properties = array(
     'id',
     'name',
     'description',
     'company'
      );

    }

    The database connection works fine if I just use the database class:

    $query = DB::query('SELECT * FROM Users');
    $result = $query->execute();

    I tried to use the ORM class like this:
    $entry = Model_User::find('first');

    But I get this error: 

    Fuel\Core\Database_Exception [ 42000 ]: 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 '."id" AS "t0_c0", "t0"."name" AS "t0_c1", "t0"."description" AS "t0_c2", "t0"."c' at line 1 with query: "SELECT "t0"."id" AS "t0_c0", "t0"."name" AS "t0_c1", "t0"."description" AS "t0_c2", "t0"."company" AS "t0_c3" FROM "users" AS "t0" ORDER BY "t0"."id" ASC LIMIT 1"
    What am I doing wrong?

    Is there a way to view the SQL statements called by ORM class to assist debugging?
  • ilNotturnoilNotturno
    Accepted Answer
    Hi mtmacdonald, if you need to debug your application you can activate the "profiling" from the configuration in the config.php file.
    To do that, simply put this line of code in your configuration file:

    'profiling' => true,

    If you want to debug your db queries, you need to insert the same line of code in the db.php configuration file.
  • HarroHarro
    Accepted Answer
    That query uses double quotes to escape column names, which MySQL doesn't like.

    Do you have 'identifier' defined somewhere in your db.php database definition? If not, it should be

    'identifier'   => '`',

    for MySQL.
  • Thanks - both helpful answers.

    The issues was the identifier being incorrect. 

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion