PDO 对两个非常相似的查询的行为不同

发布于 2024-08-05 11:31:15 字数 2270 浏览 2 评论 0原文

以下代码块工作正常(没有错误)

$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- This returns 'object'
$statement->bindParam(1, $username);

以下给出: 致命错误:在第 39 行 /file.php 中的非对象上调用成员函数 bindParam()

$email = '[email protected]';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- this returns 'boolean'
$statement->bindParam(1, $email); // -- this is line 39.

现在这很奇怪。

在我的本地计算机和远程主机上,这从来都不是问题。

此错误仅出现在我本月尝试的这家新托管公司上。难道是编译php时的配置参数?

--------编辑-------- 当我仍在试图找出问题所在时,我发现了这一点。

$query = "select userID, username from users";
$statement = $sql->prepare($query);    
$statement->execute();
$r = $statement->fetchAll(PDO::FETCH_ASSOC);

// display # of rows
echo "Rows returned: " . $statement->rowCount();

// display results array
echo '<pre>'; print_r($r); echo '</pre>'; 

在服务器上,我得到

Rows returned: 4

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

哪个是正确的。表示返回 4 行并显示结果数组。然而,在另一台服务器上,我得到了

Rows returned: 0

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

因此,似乎 PDOStatement::rowCount() 不适用于一台服务器,但适用于另一台服务器。

The following block of code works fine (no errors)

$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- This returns 'object'
$statement->bindParam(1, $username);

The following gives:
Fatal error: Call to a member function bindParam() on a non-object in /file.php on line 39

$email = '[email protected]';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- this returns 'boolean'
$statement->bindParam(1, $email); // -- this is line 39.

Now this is strange.

At my local machine, and my remote host, this was never a problem.

This errors only shows up on this new hosting company I am trying out for the month. Could it be a config param when they compiled php?

--------edit--------
While still trying to figure out what's wrong,I found this out.

$query = "select userID, username from users";
$statement = $sql->prepare($query);    
$statement->execute();
$r = $statement->fetchAll(PDO::FETCH_ASSOC);

// display # of rows
echo "Rows returned: " . $statement->rowCount();

// display results array
echo '<pre>'; print_r($r); echo '</pre>'; 

On a server, I get

Rows returned: 4

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

Which is correct. Says 4 rows returned and displays the result array. On another server however, I get

Rows returned: 0

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

Thus, it seems also that PDOStatement::rowCount() does not work on a sever but works on another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

皓月长歌 2024-08-12 11:31:15

阅读以下内容: $statement->closeCursor()

PDOStatement::closeCursor() 释放与服务器的连接,以便可以发出其他 SQL 语句

您是否在您声称没有此问题的服务器上使用相同的数据库?

Read this: $statement->closeCursor()

PDOStatement::closeCursor() frees up the connection to the server so that other SQL statements may be issued

Are you using the same database on the server where you say you don't have this issue?

氛圍 2024-08-12 11:31:15

找到了解决问题的方法。

这是整个代码块。

// check if username exists
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
$statement->bindParam(1, $username);
$statement->execute();


// check if email exists
$sql2 = new PDO('mysql:host=localhost; dbname=db', 'username', 'pw');
$query = "select * from users";
$statement = $sql2->prepare($query);
echo gettype($statement);
#$statement->bindParam(1, $email);

因此,出于某种原因,我必须创建一个新的 PDO 实例。奇怪的是,在另外两台服务器上,我不必这样做。

经过进一步观察,我发现 PDO::Prepare 引发了 PDOExeption。

就是这样:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Array
(
    [0] => HY000
    [1] => 2014
    [2] => Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
)

也许它会对将来的某人有所帮助:)

Found a solution to the problem.

This is the entire block of code..

// check if username exists
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
$statement->bindParam(1, $username);
$statement->execute();


// check if email exists
$sql2 = new PDO('mysql:host=localhost; dbname=db', 'username', 'pw');
$query = "select * from users";
$statement = $sql2->prepare($query);
echo gettype($statement);
#$statement->bindParam(1, $email);

So for some reason, I have to create a new instance of PDO. what's strange is that on 2 other servers, I don't have to do this.

And upon further looking, i found that PDO::Prepare raises an PDOExeption.

Here it is:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Array
(
    [0] => HY000
    [1] => 2014
    [2] => Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
)

Maybe it'll help someone in the future :)

清风不识月 2024-08-12 11:31:15

$email 未定义吗?您也可以尝试 var_dump($email) 看看它说什么。祝你好运。

Is $email undefined? You could try a var_dump($email) too see what it says. Good luck.

梦醒灬来后我 2024-08-12 11:31:15

您是否尝试过将 $email= 行放在 bindParam 下方(但在执行之前)? bindParam 通过引用传递参数,以便您可以作为查询执行、更改变量的值并再次执行。

我认为这可能是 PHP 设置错误。我听人们说 PDO 在 PHP 5.3 之前有很多错误,所以也许看看是否可以将 PHP 升级到最新版本?

您是否也尝试过交换这两个查询?运行一个查询后,可能会出现某些问题。

Have you tried putting the $email= line below the bindParam (but before you do the execute)? bindParam passes the parameter by reference so you can execute as query, change the value of the variable and execute again.

I think though that it's probably a PHP set up error. I've heard people saying PDO had a lot of bugs before PHP 5.3, so maybe see if you can get PHP up to the latest version?

Have you also tried swapping the two queries around? Maybe something breaks after you run one query.

别念他 2024-08-12 11:31:15

我最好推荐使用这个:

$email = '[email protected]';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));

这里不需要使用 BindParam。

I'd better recommend using this:

$email = '[email protected]';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));

No BindParam use is needed here.

陌路黄昏 2024-08-12 11:31:15

我有同样的问题
echo“返回行数:”。 $语句->rowCount();

我有 -1 行,哈哈。我的数据库是 INFORMIX 并搜索网络我发现
rowCount();仅返回相应 PDOStatement 对象执行的 DELETE、INSERT 或 UPDATE 语句中受影响的行。

对于 SELECT 语句,您需要使用函数
$语句->fetchColumn();

在这里阅读:http://www.phpbuilder.com/manual/ en/function.pdostatement-rowcount.php

I had same problem with
echo "Rows returned: " . $statement->rowCount();

I got -1 rows, lol. My database is INFORMIX and searching web i found
that rowCount(); returns only affected rows in a DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

With a SELECT statement you need to use function
$statement->fetchColumn();

read it here: http://www.phpbuilder.com/manual/en/function.pdostatement-rowcount.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文