php - 用php获取对象的值
问题描述
我有一个代码应该获取包含当前用户 IP 地址的数据库中的行数,所以我编写了一个 mysqli 语句,获取包含该 IP 地址的每一行并将其存储在一个变量中。当我打印出变量时,它打印了这个:
mysqli_result Object
(
[current_field] => 0
[field_count] => 7
[lengths] =>
[num_rows] => 2
[type] => 0
)
我如何访问num_rows
对象中的。这是我的代码:
<?php
include("../functions/functions.php");
include("./db.php");
global $conn;
$ip = get_ip();
$select = "SELECT * FROM `cart` WHERE `ip_address`= '$ip' ";
$run_check_prod = mysqli_query($conn,$select);
if(mysqli_num_rows($run_check_prod) > 0) {
print_r($run_check_prod);
}
?>
解决方案
您可以使用以下代码num_rows
通过 PHP 获取对象中的值。我实现了上述代码中用于获取对象值的代码。
<?php
include("../functions/functions.php");
include("./db.php");
global $conn;
$ip = get_ip();
$select = "SELECT * FROM `cart` WHERE `ip_address`= '$ip' ";
$run_check_prod = mysqli_query($conn,$select);
if(mysqli_num_rows($run_check_prod) > 0) {
//the $run_check_prod is object and num_rows is key
echo $run_check_prod->num_rows;
}
?>
推荐阅读
- python - 不是一个类 - TypeError: function() 接受 0 个位置参数,但给出了 1 个
- sql - SQL:如何获得足够的事件天数,使我至少有 50 个事件
- r - 我希望能够操纵“phylo”类中的对象 - 即。四舍五入/将我的引导值从小数 (.998) 转换为百分比 (99%)
- python - 修改后用匹配替换python中的子字符串
- eclipse - 使用 eGit 拉取带有已删除文件的分支
- excel - 如何测试是否设置了 Outlook,如果没有设置如何绕过
- dart - 将 Dart 2 Map 动态转换为 Javascript 对象
- elm - Elm 在 HTML 属性中放置打开的文本
- php - 从 mysql 数据库加载到 bootstrap 4 轮播中的图像
- c# - 可互换地处理存储在 BitVector32、int 和 uint 中的 32 位向量是否安全?