gpt4 book ai didi

PHP - rowCount() 给出相同的值,无论字符串是否存在

转载 作者:行者123 更新时间:2023-11-29 07:53:27 27 4
gpt4 key购买 nike

我有以下代码:

$queryMobileNumber = $dbh->prepare("SELECT mobile_number FROM $tableBusinessOwner WHERE business_owner_id = $businessOwnerIDTable");
$queryMobileNumber->bindValue( 1, $mobileNumberNew);
$queryMobileNumber->execute();

echo $businessOwnerIDTable;
echo '-';
echo $queryMobileNumber->rowCount();

我使用 PDO 连接到我的数据库。

以上代码检查用户插入的手机号码是否已存在于表中。无论该数字是否存在,当我 echo $queryMobileNumber->rowCount(); 时,该值始终为 1。

我不确定我错过了什么。我的 error_log 中没有收到任何错误。

最佳答案

正如 PHP documentation 所解释的那样

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

由于您正在执行选择,因此 rowCount() 不是您要查找的函数。

你能做什么?

<?php

$queryMobileNumber = $dbh->prepare("SELECT mobile_number FROM $tableBusinessOwner WHERE business_owner_id = $businessOwnerIDTable");
$queryMobileNumber->execute();

$res = $queryMobileNumber->fetch(PDO::FETCH_ASSOC);

echo $businessOwnerIDTable;
echo '-';
if(!empty($res['mobile_number')){
echo $res['mobile_number'); //or whatever else
} else {
echo 'N/A';
}

关于PHP - rowCount() 给出相同的值,无论字符串是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com