We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 953
    • 4 Posts
    Hi.

    Excuse my English.

    My first try with xPDO and I’ve got a ’Error preparing statement for query: ’...

    Could someone help me?

    So, I have the following scenario:

    1) A database called db_learning with just one table called lear_user, that has two fields: id and name. The first is a ’int not null primary key auto_increment’, and the second a ’varchar(100)’.

    2) Three files: learning.mysql.schema.xml, create_everything.php and do_things.php.

    Here’s the contents:

    -- learning.mysql.schema.xml --

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="learning" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" tablePrefix="lear_">
        <object class="User" table="user" extends="xPDOSimpleObject">
            <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        </object>
    </model>
    


    -- create_everything.php --

    include('./xpdo/xpdo.class.php');
    
    $dsn = 'mysql:host=localhost,dbname=db_learning';
    $xpdo = new xPDO($dsn, $username='root', $password='password');
    $xpdo->setDebug(true);
    
    $xpdo->setLogLevel(xPDO::LOG_LEVEL_INFO);  
    $xpdo->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');  
    
    $manager = $xpdo->getManager();
    $generator = $manager->getGenerator();
    
    $schema = '/var/www/localhost/htdocs/php/learning.mysql.schema.xml';
    $target = '/var/www/localhost/htdocs/php/model/';
    
    $generator->parseSchema($schema,$target);
    


    * After that, the directory ’model’ is filled with some files that seems to be all ok.


    -- do_things.php --

    include('./xpdo/xpdo.class.php');
    
    $dsn = 'mysql:host=localhost,dbname=db_learning';
    $xpdo = new xPDO($dsn, $username='root', $password='password');
    $xpdo->setDebug(true);
    $xpdo->setLogLevel(xPDO::LOG_LEVEL_INFO);  
    $xpdo->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
    
    $xpdo->addPackage('learning', '/var/www/localhost/htdocs/php/model/','lear_');
    
    $usuario = $xpdo->newObject('User');
    
    $user_1 = $xpdo->getObject('User', array('id'=>19) );
    
    print_r($user_1);



    And... I get the following error:

    [2010-04-21 20:27:32] (ERROR @ /php/do_things.php)
    
    Error preparing statement for query: SELECT `User`.`id` AS `User_id`, `User`.`name` AS `User_name` FROM `lear_user` AS `User` WHERE `User`.`id` = ? 



    The funny stuff is I can copy and paste that SQL, change ’?’ with ’19’ and that works fine, it returns the table’s record in a MySQL console.


    So, what could be my mistake?

    []s
    Alexander
    Brazil
    • Your DSN string is wrong, and I suspect you are not getting connected to the actual database as a result:
      $dsn = 'mysql:host=localhost,dbname=db_learning';

      should be:
      $dsn = 'mysql:host=localhost;dbname=db_learning';

      Note the semicolon instead of the comma.
        • 953
        • 4 Posts
        Yep, you right.

        Thank you very much!!

        []s
        Alexander
        Brazil
          • 1169
          • 312 Posts
          I am a bit confused

          $dsn = 'mysql:host=localhost,dbname=db_learning';

          Is used in
          -- create_everything.php --

          and this code appears to work

          * After that, the directory ’model’ is filled with some files that seems to be all ok.
          The same code then fails in
          -- do_things.php --

          $dsn = 'mysql:host=localhost,dbname=db_learning';

          should be:
          $dsn = 'mysql:host=localhost;dbname=db_learning';

          Note the semicolon instead of the comma.

          Am I right huh or missing something.
            DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31
          • it works cause the first steps do not actually work with the database. You had an xPDO connection to the server without a database selected most likely.
              • 1169
              • 312 Posts
              Now I see thankyou @OpenGeek I am now not so confused.
                DEVELOPMENT ENV:- Ubuntu 12.04 | MODx Revolution 2.2.8 | LAMP 2i Apache 2.2.22 | Php 5.3.10 | Mysql 5.5.31 MySQL client version: 5.5.31