2

I have followed the zend instructions for implement my web Authentication using a database table.

It's exactly the same code, but when render the page, the following exceptions appears:

    Zend\Authentication\Adapter\Exception\RuntimeException
    File:
    C:\xampp\htdocs\pfc\vendor\ZF2\library\Zend\Authentication\Adapter\DbTable.php
    Mensaje:
    The supplied parameters to DbTable failed to produce a valid sql statement, please
    check table and column names for validity.

produced by this other:

    Zend\Db\Adapter\Exception\InvalidQueryException
    File:
    C:\xampp\htdocs\pfc\vendor\ZF2\library\Zend\Db\Adapter\Driver\Mysqli\Statement.php
    Mensaje:
    Statement couldn't be produced with sql: SELECT `users`.*, (CASE WHEN `password` = ?
    THEN 1 ELSE 0 END) AS `zend_auth_credential_match` FROM `users` WHERE `mail` = ?

Seems to be that Statement.php can not execute the sql of above, but I send the sql by phpmyadmin replacing the ? for strings and work ok.

I am sure that $dbAdapter works ok also because I have tested it and the columns name are "mail" and "password".

This in my code, also I put the $dbAdapter test code.

    $dbAdapter = new DbAdapter(array(   //This DbAdapter Work ok sure!!
        'driver' => 'Mysqli',
        'database' => 'securedraw',
        'username' => 'root',
        'password' => ''
    ));

    $fp = function($name) use ($dbAdapter) { return $dbAdapter->driver->formatParameterName($name);};
    $sql = 'SELECT * FROM ' . $qi('users') . ' WHERE id = ' . $fp('id');
    $statement = $dbAdapter->query($sql);
    $parameters = array('id' => 1);
    $sqlResult = $statement->execute($parameters);
    $row = $sqlResult->current();
    $mail = $row['mail'];
    $password = $row['password'];  //until here test $dbAdapter exitly!!

    //Start the auth proccess!!
    $authAdapter = new AuthDbTableAdapter($dbAdapter);
    $authAdapter->setTableName('users')
    ->setIdentityColumn('mail')
    ->setCredentialColumn('password');
    $authAdapter->setIdentity('josep')
    ->setCredential('josep');
    $authResult = $authAdapter->authenticate();   //This is the fail method!!!
josepmra
  • 617
  • 9
  • 25

2 Answers2

3

After more research on the subject, I discovered that if changed the driver of the dbAdapter to pdo_mysql, authenticate method works ok.

The problem is I don't want use PDO because the SGBD won't change in the future.

Somebody know because happen this?

josepmra
  • 617
  • 9
  • 25
0

This may seems old but I was able to solve this error. This error is caused from you MySQL version.

This one works for me. All you need to do is to remove the driver_options from your db setup, this code is usually located at your global.php or .local.php from your Config file.

Change FROM:

'db' => array(
    'driver'         => 'Pdo_Mysql',
    'dsn'            => 'mysql:dbname=dbName;host=localhost',
    'username'       => 'dbUser',
    'password'       => 'dbPass',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),

TO

'db' => array(
    'driver'         => 'Pdo_Mysql',
    'dsn'            => 'mysql:dbname=dbName;host=localhost',
    'username'       => 'dbUser',
    'password'       => 'dbPass',
),

Thank you. This solution solved my problem.