fatal error: uncaught error: can not use object of type mysqli_result as array

0

I have this problem when calling the function to dump the data I want.

I have created a function to list some items according to their id:

function list_item_by_id_controller(){
    global $link, $gf_query;

    $id_types = array('eid','hid','pid','poid','bid');

    $getKeys = array_keys($_GET);

    foreach ( $getKeys as $k ){
        if( in_array($k, $id_types) ){
            $id = $_GET[$k]; 
        }
    }

    if(!$id){
        $gf_query['message_code'] = 'i101';
            return false;
    }

    $query = list_item_by_id($id);

    if( !$query ){
        $gf_query['message_code'] = 'i102';
        return false;
    }

    return $query;
}

When I call the function on the page where I want to print the data, I get the following error:

fatal error: uncaught error: cannot use object of type mysqli_result as array in c:\mamp\htdocs\gourfood_02-08__modificado\public_html\templates\product-single.php:8 stack trace: #0 c:\mamp\htdocs\gourfood_02-08__modificado\public_html\single.php(23): include() #1 {main} thrown in c:\mamp\htdocs\gourfood_02-08__modificado\public_html\templates\product-single.php on line 8

On line 8 of product-single.php I have the following line of code <h2 class="title"> <?php echo $i['item'] ?></h2>

I have tried changing in the model mysqli_num_rows by mysqli_fetch_array if that was the problem but nothing.

This is the model

function list_item_by_id($id){
  global $link;

  $id = (int)$id;

  $sql = "SELECT * FROM items
          INNER JOIN categorias ON categorias_id = categorias.id
          WHERE items.id = $id";

  $query = mysqli_query($link,$sql);

  $num_rows = mysqli_num_rows($query);

  if( $num_rows > 0 ){
    return $query;
  }

  return false;
}

Could someone tell me what's wrong? Thanks

EDIT: the code around echo $i['item] is the following: On the page single.php

<?php 
require('load.php');

require('templates/header.php');
require('templates/navbar.php');

$i = list_item_by_id_controller();

?>
    <div class="single-main">
      <div class="container">

        <div class="row">
          <ol class="breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><a href="index.php#carta">Carta</a></li>
            <li class="active">Producto</li>
          </ol>

          <?php if($i): ?>
            <?php include('templates/product-single.php') ?>
          <?php else: ?>
            <div class="row">
              <div class="col-xs-12">
                <p>Oooppps, parece que ha habido un error. Si lo deseas, puedes volver a la portada pinchando <a href="index.php"> aquí</a></p>
              </div>
            </div>
          <?php endif; ?>
        </div>
      </div>
    </div>

Then in product-single.php is where I include the echos to dump the data:

<article class="product-single">
    <div class="col-xs-12 col-md-6">
        <img src="images/03-small.jpg" class="img-responsive portfolio-img" alt="Project Title">
    </div>

    <div class="col-xs-12 col-md-6">
        <div class="product-detail">
            <h2 class="title"> <?php echo $i['item'] ?></h2>
            <h4 class="price"><?php echo $i['precio'] ?> €</h4>
            <p class="descripcion"><?php echo $i['descripcion'] ?></p>

            <form method="POST" id="orderForm">
                <div class="form-group">
                    <label for="cantidad">Cantidad</label>
                    <input type="number" class="form-control" id="cantidad" value="1">
                </div>
                <div id="success"></div>
                <button type="submit" class="btn btn-custom btn-lg">Enviar</button>
            </form>
        </div>
    </div>
</article>
    
asked by Silvia Pena 22.09.2017 в 19:05
source

1 answer

0

The error tells you . You try to use an object as an array. It is good to check if a variable that is expected to be an array is really an array.

You can use var_dump () on the object that is generating the error to see if it has data and how to get them out for what you need.

Then you have some things that are prone to make mistakes. The function list_item_by_id_controller () operates on $ _ GET directly, you expect it to contain an array, although it can have anything ... you can rewrite its contents at any point in the program, I would pass the array as an argument to the function or I would use a safeguard clause recovering all its contents ...

function list_item_by_id_controller(){
    global $link, $gf_query;

    $arr = $_GET;
    if ( ! is_array( $arr )) {

        //Código que te indique que no tienes lo esperado

        return;
    }

    $id_types = array('eid','hid','pid','poid','bid');


    $getKeys = array_keys( $arr );

    foreach ( $getKeys as $k ){
        if( in_array($k, $id_types) ){
            $id = $arr[ $k ]; 
        }
    }

    //Resto de código...

Using global I do not like anything either ...

Then in the same function, the variable $ id is assigning it within a conditional , inside a foreach , what if is never assigned a value? You use it below, you should initialize it to NULL, before the foreach.

I did not keep looking at code, I have things to do now.

Greetings.

    
answered by 23.09.2017 / 12:02
source