illegal string offset PHP

2

If you know how I could resolve this, I would appreciate it very much:

PROBLEM:

  

illegal string offset 'index (can be "id" or another cualqiera)'

This is coming to me when I try to get information from the database that stores my information array from my database,

CODE:

Customer profile.php

<?php 
      $columna = "codigo";
      $valor3 = $_GET["ruta"];
      $cliente = ControladorClientes::MostrarClientes($columna, $valor3);//funcionando

      $item2 = "id_cliente";
      $valor2 = $cliente["id"];

      $contarLocalidades = ControladorLocalidades::ContarRegistros($item2, $valor2);//funcionando

      $localidades = (array) ControladorLocalidades::MostrarLocalidades($item3="id", $valor3="1");//illegar string offset
      foreach($localidades as $key => $value){
        echo '<div>'.$value['id_cliente'].'</div>';
      }


echo '
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Perfil de Cliente
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fas fa-tachometer-alt"></i> Inicio</a></li>
        <li class="active">'.$cliente["codigo"].'</li>
      </ol>
    </section>
<section class="invoice">
      <!-- title row -->
      <div class="row">
        <div class="col-xs-12">
          <h2 class="page-header">
            <i class="fa fa-cube"></i> '.$cliente["nombre"].'.
            <small class="pull-right">Fecha de creacion: '.$cliente["fecha_creacion"].'</small>
          </h2>
        </div>
        <!-- /.col -->
      </div>
      <!-- info row -->
      <div class="row invoice-info">
        <div class="col-sm-4 invoice-col">
        <h3>Datos del Cliente</h3>
          <address>
            <strong>'.$cliente["nombre"].'</strong><br>
            '.$cliente["rnc"].'<br>
            '.$cliente["direccion"].'<br>
            ciudad: '.$cliente["ciudad"].'<br>
            tel: '.$cliente["telefono_1"].'
          </address>
        </div>
        <!-- /.col -->
        <div class="col-sm-4 invoice-col">
          <h3>Datos del contacto</h3>
          <address>
            <strong>'.$cliente["persona_contacto"].'</strong><br>
            email: '.$cliente["email"].'<br>
            tel: '.$cliente["telefono_2"].'<br>

          </address>
        </div>
        <!-- /.col -->
        <div class="col-sm-4 invoice-col">
          <b>Código #'.$cliente["codigo"].'</b><br>
          <br>
          <b>Localidades activas:</b> '.$contarLocalidades["total"].'<br>
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

      <!-- Table row -->
      <div class="row">
        <div class="col-xs-12 table-responsive">
          <table class="table table-striped">
            <thead>
            <tr>
              <th>#</th>
              <th>Código</th>
              <th>Nombre</th>
              <th>Equipos</th>
              <th>Acción</th>
            </tr>
            </thead>
            <tbody>
            <tr>';


            echo '</tr>
            </tbody>
          </table>
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

      <div class="row">
        <!-- accepted payments column -->
        <div class="col-xs-6">
          <p class="lead">Payment Methods:</p>
          <img src="../../dist/img/credit/visa.png" alt="Visa">
          <img src="../../dist/img/credit/mastercard.png" alt="Mastercard">
          <img src="../../dist/img/credit/american-express.png" alt="American Express">
          <img src="../../dist/img/credit/paypal2.png" alt="Paypal">

          <p class="text-muted well well-sm no-shadow" style="margin-top: 10px;">
            Etsy doostang zoodles disqus groupon greplin oooj voxy zoodles, weebly ning heekya handango imeem plugg
            dopplr jibjab, movity jajah plickers sifteo edmodo ifttt zimbra.
          </p>
        </div>
        <!-- /.col -->
        <div class="col-xs-6">
          <p class="lead">Amount Due 2/22/2014</p>

          <div class="table-responsive">
            <table class="table">
              <tbody><tr>
                <th style="width:50%">Subtotal:</th>
                <td>$250.30</td>
              </tr>
              <tr>
                <th>Tax (9.3%)</th>
                <td>$10.34</td>
              </tr>
              <tr>
                <th>Shipping:</th>
                <td>$5.80</td>
              </tr>
              <tr>
                <th>Total:</th>
                <td>$265.24</td>
              </tr>
            </tbody></table>
          </div>
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

      <!-- this row will not appear when printing -->
      <div class="row no-print">
        <div class="col-xs-12">
          <a href="invoice-print.html" target="_blank" class="btn btn-default"><i class="fa fa-print"></i> Print</a>
          <button type="button" class="btn btn-success pull-right"><i class="fa fa-credit-card"></i> Submit Payment
          </button>
          <button type="button" class="btn btn-primary pull-right" style="margin-right: 5px;">
            <i class="fa fa-download"></i> Generate PDF
          </button>
        </div>
      </div>
    </section>
</div>';

CONTROLLER:     

class ControladorLocalidades{

    static public function MostrarLocalidades($item, $valor){

        $tabla = "localidades";

        $respuesta = ModeloLocalidades::MostrarLocalidades($item, $tabla, $valor);

        return $respuesta;
    }
}

MODEL:

<?php

require_once "bdd.php";

class ModeloLocalidades{

    static public function MostrarLocalidades($item, $tabla, $valor){

        if($item != null){

            $stmt = Conexion::Conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item");

            $stmt-> bindParam(":".$item, $valor, PDO::PARAM_STR);

            $stmt -> execute();

            return $stmt -> fetch();

        }else{

            $stmt = Conexion::Conectar()->prepare("SELECT * FROM $tabla");

            $stmt -> execute();

            return $stmt -> fetchAll();

        }


        $stmt -> close();

        $stmt = null;
    }
}

DATABASE

IMPORTANT NOTE:  WHEN I CHARGE A VAR_DUMP($localidades) shows me the complete array, when I try to filter it by the indexes (id, name, etc) it gives me the error of string offset.

thanks in advance for the help

    
asked by vickry 18.08.2018 в 19:33
source

2 answers

1

The problem was in the model, but not in the construction of the SELECT request, it was about how it was receiving the data:

In other words, using fetch is only forcing to receive a single value, while it was requesting that it return more than 1, by making this change.

        }elseif($condicionActual!= null && $nuevaCondicion!= null) {

        $stmt = Conexion::Conectar()->prepare("SELECT * FROM $tabla WHERE $item = :$item");

        $stmt-> bindParam(":".$condicionActual, $value, PDO::PARAM_STR);

        $stmt -> execute();

        return $stmt -> fetchAll(); //Antes era return $stmt -> fetch

    }
    
answered by 20.08.2018 в 04:59
0

I think your error is in the model, change the query lines to:

$stmt = Conexion::Conectar()->prepare("SELECT * FROM :tabla WHERE :item = :value");

$stmt-> bindParam(":tabla", $tabla, PDO::PARAM_STR);
$stmt-> bindParam(":item", $item, PDO::PARAM_STR);
$stmt-> bindParam(":value", $valor, PDO:: PARAM_INT);
    
answered by 18.08.2018 в 20:45