php - 无法在 php 中正确使用 sql 查询的结果
问题描述
我想要做的是获取满足特定要求的行数,并且根据执行的数量,脚本会做不同的事情。
我的代码:
$sql2 = "SELECT COUNT(email)FROM subscribers WHERE id= ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param("s", $id);
$stmt2->execute();
$callbacks = $stmt2->get_result();
while($rows = $callbacks->fetch_assoc()){
$results = $rows;
}
$stmt2->close();
if ($results <2001){
$email = trim($_POST["email"]);
}else{
$email_err = "This user has reached the limmit to what their plan can allow .";
}
我遇到的问题是我不确定如何实际使用行数。
在这种配置中,它不关心结果的数量,它只是发布它们。
如果我做这样id_err = $results;
的事情来查看查询结果是什么,它会给我一个数组到字符串的转换错误。
这种操作听起来像硬件密集型,如果您认为应该直接查询行并使用 PHP 计算它们,请告诉我如何。
我也尝试过$results = $rows['count']
使用不同的名称(例如“total”和“result”)进行一些变体,但它们仍然会发布。当我尝试查看带有它的查询结果时,id_err = $results;
它会完全忽略它并发布。
为了避免混淆 $id_err 是什么,它会在脚本被使用时停止发布。
并澄清这个 SQL 查询是在一个条件语句中。
先感谢您。
解决方案
该count
函数将所有记录作为一行返回,计数..因此删除while
并比较适当的索引。
$sql2 = "SELECT COUNT(email) as the_count FROM subscribers WHERE id= ?";
$stmt2 = $conn->prepare($sql2);
$stmt2->bind_param("s", $id);
$stmt2->execute();
$callbacks = $stmt2->get_result();
$rows = $callbacks->fetch_assoc();
$stmt2->close();
if ($rows['the_count'] < 2001){
$email = trim($_POST["email"]);
}else{
$email_err = "This user has reached the limmit to what their plan can allow .";
}
推荐阅读
- ionic-framework - 离子蓝牙打印机 StarPRNT 提示成功但不打印 TSP100BI
- spring-data-rest - 如何将 Spring Data Rest 与弹性搜索 CriteriaQuery 一起使用?
- r - data.frame 的转置
- javascript - 通过迭代具有不同键值对的单个对象数组来制作 flatList 的最佳方法
- cpanel - 当我尝试从某些设备访问我的网站时,它显示此错误“由于管理员操作而被阻止”
- after-effects - After Effects - 随着修剪路径进展到特定位置,在指定路径上自动显示 Comps(地名)
- windows-server-2019 - Windows server 2019 DHCP 池在连接到某些无线 SSID 时无法发出 IP 地址
- typescript - 将多个泛型类型包装到“泛型命名空间”中
- javascript - 柜台有什么问题?
- azure - 如何在 Azure 仪表板中设置外部状态页面?