Php pdo ¿What am I doing wrong?

I am learning php pdo; my environment: NetBeans 6.5.1, XAMPP 1.7.0, and I have this code that seems to hook up.

  • If I change dbname to non-existent it throws "db does not exist" exception
  • If I change user it calls "invalid login")

but when i call $ cn-> query it calls:

An unhandled Win32 exception occurred in apache.exe

What am I doing wrong?

$hostname = 'localhost';
$username = 'crb';
$password = 'letmein';
try {
    $cn = new PDO("mysql:host=$hostname;dbname=bitacora", $username, $password);
    echo 'Connected to database<br />';
    $sql = "SELECT * FROM usuario WHERE login = '".$login."' AND clave = '".$clave."'";
    // Error here
    foreach ($cn->query($sql) as $row) {
        print $row['login'] .' - '. $row['clave'] . '<br />';
    }
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}

      

+1


source to share


3 answers


This is a bug in XAMPP 1.7.0. Upgrade to 1.7.1 or follow these instructions to fix your 1.7.0 installation.



+4


source


Is $ cn valid? Check the return value. What you have described so far does not convince me that you are connected.



0


source


If you haven't already done so, I would make sure your environment is working correctly.

  • Make sure the user is running MySQL itself (using something like mysqlquery).
  • Make sure php can connect to MySQL. I set phpmyadmin to all new settings (even if I don't leave it in place) to make sure I have a good working connection.
  • Have PDO Beyond Error Exceptions (see http://us2.php.net/manual/en/pdo.error-handling.php ) to see where errors occur.

I am assuming that you never connect to MySQL, which explains the inability to modify the database.

0


source







All Articles