Query mysql is not giving return

0

I have a database Registration with two tables joao and maria and old columns and I'm trying to make a query in php just do not know if I'm doing it right because I've been using the database for some time 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 nothing is returning

    
asked by anonymous 05.06.2016 / 02:57

2 answers

0

Errors are probably not being displayed.

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

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

I recommend that you read Settings in PHP Run

In addition, since the search is for a string, the same, in your case the variable $consult , must be enclosed in quotation marks:

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

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

    
07.06.2016 / 04:24
0

When using MySQL, remember that most of the functions in the first argument are the connection, and do not forget the i at the end.

The isset must be called before using some array key and not after the contrary will generate a warning.

Always use mysqli_error($conexao) function together mysqli_query() to return any query errors to make it easier to fix 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 mysqli_real_escape_string() to prepared statements, see how to in this answer .

    
07.06.2016 / 15:22