I'm learning php pdo; my environment is : NetBeans 6.5.1, XAMPP 1.7.0 and I have this code, which it seems to connect.
- If I change dbname to a non existent one, it raises exception "db not exists"
- If I change user, it raises "login incorrect")
but when I call $cn->query, it raises:
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();
}