SilverSurfer SilverSurfer - 15 days ago 4x
MySQL Question

PHP. Undefined property: stdClass in a double query

The second query does not work and Im trying to echo all in the information in the second query from another table but does appear the next message:

Undefined property: stdClass::$titulo
(and the rest of "resultado2")

I tried to do a while like the first query (while ($nfila = $resultado2->fetch_object())) but does appear another error message:

Trying to get property of non-object

$usuario = $_SESSION['usuario_valido'];
$consulta = "SELECT * FROM `lista-usuarios` WHERE categoria='favoritos' AND nombreusuario='$usuario' ORDER BY pelicula ASC";
$resultado = $conexion->query($consulta);

if ($resultado)
$cont3 = 1; //Para el id de la peli
$i = 0; //Para un class unico
while ($nfila = $resultado->fetch_object())
$pelicula = "peli" . $cont3; //Para hacer un id unico de cada pelicula
echo "<form role='form' id='$pelicula'><table class='table table-striped'>";
echo "
<td>" . $cont3 . "</td>
<td class='celda2'>" . $nfila->pelicula . "</td>
<td class='celda2'>" . $nfila->anio . "</td>
<td class='celda2'><button type='button' class='infoCompleta btn btn-default' data-toggle='modal'data-target='.modal" . $i . "'><span class='glyphicon glyphicon-hand-right'></span> Info Completa</button></td><td class='celda2'><button type='button' class='eliminarFav1 btn btn-danger'><span class='glyphicon glyphicon-remove-sign'> </span> Favoritos</button></td>
$consulta2 = "SELECT * FROM `peliculas` WHERE titulo='$nfila->pelicula'";
$resultado2 = $conexion->query($consulta2);
if ($resultado2)
echo "
<div class='modal fade bs-example-modal-lg modal" . $i . "' tabindex='-1' role='dialog' aria-labelledby='myLargeModalLabel'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<button tyle='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
<h4 class='modal-tigle'>" . $nfila->titulo . "</h4>
<div class='modal-body'>
<center><a target='_blank' href='imagenes/" . $nfila->imagen . "'>
<img class='img-top-peliculas' src='imagenes/" . $nfila->imagen . "' class='img-rounded '></a>
</center><br />
<p><strong>Titulo Original: </strong>" . $nfila->tituloOriginal . "</p>
<p><strong>Año: </strong>" . $nfila->anio . "</p>
<p><strong>Duracion: </strong>" . $nfila->duracion . " min.</p>
<p><strong>País: </strong>" . $nfila->pais . "</p>
<p><strong>Director: </strong>" . $nfila->director . "</p>
<p><strong>Reparto: </strong>" . $nfila->reparto . "</p>
<p><strong>Género: </strong>" . $nfila->genero . "</p>
<p><strong>Sinopsis: </strong>" . $nfila->sinopsis . "</p>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Cerrar</button>

echo "<input name='usuario' type='hidden' value='$usuario'>";
echo "<input name='titulo' type='hidden' value='$nfila->pelicula'>";
echo "<input name='anio' type='hidden' value='$nfila->anio'>";
echo "<input name='categoria' type='hidden' value='categoria' class='cat'>";
echo "</table></form>";

echo "<span class='datos'></span>";
echo "Hubo un problema al cargar las peliculas";

Ray Ray

You're not fetching the results from $resultado2 = $conexion->query($consulta2);. You are still using the last $nfila from the earlier $nfila = $resultado->fetch_object(). I'm going to assume you want something like:

   if ($resultado2) {
        $nfila2 = $resultado2->fetch_object();

Then use $nfila2 in that inner conditional where you echo out the other portion.

  echo "
        ... stuff ....

      <h4 class='modal-tigle'>" . $nfila2->titulo . "</h4>

       ... more stuff....";

Also, I don't recommend nested queries on the same connection. I'd do a fetch all on the original set:

 $results =  $resultado->fetch_all();
 foreach($results as $nfila) {
       //Do stufff

However, fetch_all only return arrays in mysqli, not objects so, just make sure you convert the $nfila->pelicula referneces to $nfila['pelicula']