Mysql and PHP Comparison SUBQUERY

Asked

Viewed 28 times

0

I made a query to select all user records of a table, but I inserted a filter where the admin selects the state and filters only the user records of the selected UF, but is giving error in return.

<?php
if( !isset( $_GET['uf'] ) ) {

## Assim funciona corretamente, retorna todos registros de todos usuários
$sql = "SELECT r.* FROM `registros` FROM `registros` r WHERE r.`registro` = 'A' ORDER BY r.`registros_data_cad` DESC";

} else if ( isset( $_GET['uf'] ) ) { 

## Assim seleciona apenas registros de um UF, ex: AC
$where_clause = "AND ( SELECT u.`user_uf` FROM `user` u WHERE u.`user_uf` = '" . mysqli_real_escape_string( $mysqli, $_REQUEST['uf'] ) . "' )";

$sql = "SELECT r.* FROM `registros` FROM `registros` r " WHERE r.`registro` = 'A' " . $where_clause . " ORDER BY r.`registros_data_cad` DESC";

}
?>

I tried that way but it’s a mistake, could give me a help here?

Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, Boolean Given in /home/atc2018/public_html/balance.php on line 106;

Pasting in phpmyadmin gives error #1054, I consulted and is nonexistent column, however the column exists.

  • What would be the mistake? Failed to report this only :)

1 answer

2


I think the problem is in the "." points of yours query.

Try changing your variable $sql as follows:

$sql = "SELECT r.* FROM `registros` r WHERE r.`registro` = 'A' ". $where_clause ." ORDER BY r.`registros_data_cad` DESC";

  • Sorry, the error of the point was to insert here, but the point exists. I supplemented the question.

  • @Angeloj Read this answer more carefully... You edited your question, but this answer still fixes your problem!

  • that, truth, had not repaired the amendment beyond the points, thank you!

  • No more error, but returns 0 records. But in the user table the UF field is AC and in the records have users of UF Acre.

  • Then you’ll have to validate your query SQL.

Browser other questions tagged

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