Skip to content
Advertisement

getting the value of an object with php [closed]

i have a code that is supposed to get the number of rows in a database that contains the ip address of the current user, so i wrote a mysqli statement that gets every row that contains that ip address and stored it in a variable. When i printed out the variable it printed this:

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 7
    [lengths] => 
    [num_rows] => 2
    [type] => 0
)

how can i access the num_rows in the object. here is my code:

 <?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);
    } 
 ?>

Advertisement

Answer

You can use the below code for getting the value num_rows in an object with PHP. I implemented the code used in the above code for getting value in the object.

<?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;
} 
?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement