Empty PHP and MySql Array

0

I am learning how to program in php, and I found a problem when bringing data from MySql. The error, or problem, is that it does not bring any data from the "measurement" table.

I am using WAMP, and the code is as follows:

<?php 
$enlace = mysqli_connect("localhost", "root", "", "db_evasion");
if (!$enlace) {
    echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_errno() . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
    exit;
}   

$query = "SELECT * FROM medicion";

var_dump($query);
$resultado = $enlace->query($query);
var_dump($resultado);
echo "Retorno: ".count($resultado);

?>
    
asked by Luippo 22.08.2017 в 15:53
source

1 answer

0

The problem is that you are working with mysqli as procedural and you should continue with that method if you prefer it instead of treating it as an object by calling the query method of the link.

<?php

$enlace = mysqli_connect("localhost", "root", "", "db_evasion");
if (!$enlace) {
  $mysqli_connect_error = "Error: No se pudo conectar a MySQL.".PHP_EOL;
  $mysqli_connect_error .= "error de depuración: " . mysqli_connect_errno() . PHP_EOL;
  $mysqli_connect_error .= "error de depuración: " . mysqli_connect_error() . PHP_EOL;
  die($mysqli_connect_error);
}

$query = "SELECT * FROM medicion";
$resultados = mysqli_query($enlace, $query);

if (mysqli_num_rows($resultados) <= 0) {
  echo "No hay resultados de medición.";
} else {
  while($resultado = mysqli_fetch_assoc($resultados)) {
    // ...
  }
}

mysqli_close($enlace);
    
answered by 22.08.2017 / 16:03
source