首页 > 解决方案 > 用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);
    } 
 ?>

标签: php

解决方案


您可以使用以下代码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;
} 
?>

推荐阅读