Mysql query not returning

Asked

Viewed 202 times

0

i have a database Register two-table Joao and maria containing two columns name and age and I’m wanting to do a query in php I just don’t know if I’m doing it right because I’ve been using database I have the following code:

<?php
        
  $host = 'localhost';
  $user = 'devel';
  $password = '********';
  $database = 'Cadastro';
        
  $connect = mysqli_connect($host, $user, $password, $database) or die(mysqli_error());
  $consult = $_GET['q'];
            
  if(isset($_GET['q'])){
      $sql = mysql_query("SELECT * FROM `Cadastro` WHERE `nome` LIKE $consult");
      echo $sql;
  }                    
  else {
      echo 'nao foi encontrado nada';
  }
?>

I’m not getting any feedback

there is already data in the columns but it is not returning anything

  • There’s something missing from this code, um i in the mysql_query() and pick up the query result with a while & mysqli_fetch_assoc()

  • As @rray said: $result = mysqli_query( $connect, "SELECT * FROM ... and print_r( mysqli_fetch_assoc( $result ) ) to see the return. I liked the names of the example :) "João e Maria" are very didactic names for examples here on the site.

  • And before they erase all your DB, $consult = mysql_real_escape_string( $conexao, $_GET['q'] );

  • I think my consuta this wrong is not returning anything yet there is some way to know if my consuta was performed successfully?

  • It is not even being performed, it has to correct the serious errors before. Reread the previous comments.

2 answers

0


Probably errors are not being displayed.

If you are in a (non-public) development environment, you can change your file php.ini changing:

  • where there is display_errors = off change to display_errors = on
  • where there is display_startup_errors = off change to display_startup_errors = on.
  • where there is error_reporting = QUALQUER_COISA change to error_reporting = E_ALL

I recommend that you read on Settings in PHP Running

Also, as the search is for a string, the same, in your case the variable $consult, should be in quotes:

$sql = mysql_query("SELECT * FROM `Cadastro` WHERE `nome` LIKE '$consult'");

I hope to have helped and wish you good luck in your project!

0

When using Mysqli remember that most functions the first argument is the connection and don’t forget the i in the end.

The isset should be called before using any array key and not after the other will generate a Warning.

Always uses function mysqli_error($conexao) together mysqli_query() to return the errors of some query to facilitate the correction of the problem.

$connect = mysqli_connect($host, $user, $password, $database);
$consult = isset($_GET['q']) ? mysqli_real_escape_string($connect, $_GET['q']) : '';

$query = mysqli_query($connect, "SELECT * FROM `Cadastro` WHERE `nome` LIKE %$consult%") or die(mysqli_error($connect);
while($row = mysqli_fetch_assoc($query)){
    echo $row['nome'] .' - '. $row['idade'] .'<br>';
}

The ideal solution is to change the mysqli_real_escape_string() by Prepared statements, see how to do in that reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.