phppdo¿我做错了什么?

时间:2022-09-06 21:37:09

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.

我正在学习php pdo;我的环境是:NetBeans 6.5.1,XAMPP 1.7.0和我有这个代码,它似乎连接。

  • If I change dbname to a non existent one, it raises exception "db not exists"
  • 如果我将dbname更改为不存在的,则会引发异常“db not exists”

  • If I change user, it raises "login incorrect")
  • 如果我更改用户,则会引发“登录错误”)

but when I call $cn->query, it raises:

但是当我调用$ cn-> query时,它会引发:

An unhandled Win32 exception occurred in apache.exe

apache.exe中发生未处理的Win32异常

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();
}

3 个解决方案

#1


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.

这是XAMPP 1.7.0中的一个错误。升级到1.7.1或按照这些说明修复1.7.0安装。

#2


Is $cn valid? Check the return value. What you've described so far doesn't convince me that you're connected.

$ cn有效吗?检查返回值。到目前为止你所描述的并没有让我相信你已经联系了。

#3


If you haven't already, I'd make sure your environment was working right.

如果您还没有,我会确保您的环境正常运行。

  1. Check to make sure the user works with MySQL itself (using something like mysqlquery).
  2. 检查以确保用户使用MySQL本身(使用类似mysqlquery的东西)。

  3. Check to make sure php can connect to MySQL. I install phpmyadmin on all new setups (even if I don't leave it in place) to make sure I have a good working connection.
  4. 检查以确保php可以连接到MySQL。我在所有新设置上安装了phpmyadmin(即使我没有留下它),以确保我有一个良好的工作连接。

  5. Have PDO through exceptions on errors (see http://us2.php.net/manual/en/pdo.error-handling.php) to see where the errors are occurring right away.
  6. 让PDO通过错误例外(参见http://us2.php.net/manual/en/pdo.error-handling.php)来查看错误发生的位置。

My guess is that you are not ever connecting to MySQL, which would explain the inability to change the database.

我的猜测是你没有连接到MySQL,这可以解释无法更改数据库。

#1


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.

这是XAMPP 1.7.0中的一个错误。升级到1.7.1或按照这些说明修复1.7.0安装。

#2


Is $cn valid? Check the return value. What you've described so far doesn't convince me that you're connected.

$ cn有效吗?检查返回值。到目前为止你所描述的并没有让我相信你已经联系了。

#3


If you haven't already, I'd make sure your environment was working right.

如果您还没有,我会确保您的环境正常运行。

  1. Check to make sure the user works with MySQL itself (using something like mysqlquery).
  2. 检查以确保用户使用MySQL本身(使用类似mysqlquery的东西)。

  3. Check to make sure php can connect to MySQL. I install phpmyadmin on all new setups (even if I don't leave it in place) to make sure I have a good working connection.
  4. 检查以确保php可以连接到MySQL。我在所有新设置上安装了phpmyadmin(即使我没有留下它),以确保我有一个良好的工作连接。

  5. Have PDO through exceptions on errors (see http://us2.php.net/manual/en/pdo.error-handling.php) to see where the errors are occurring right away.
  6. 让PDO通过错误例外(参见http://us2.php.net/manual/en/pdo.error-handling.php)来查看错误发生的位置。

My guess is that you are not ever connecting to MySQL, which would explain the inability to change the database.

我的猜测是你没有连接到MySQL,这可以解释无法更改数据库。