Nikk 17016 Nikk 17016 - 6 months ago 11
HTML Question

Error "mysqli_num_rows"

How do I fix this error ?

edit.php:



<?php
include 'connect.php';

if(isset($_POST['btn_submit'])){

}
$cpf = '';
$nome = '';
$cep = '';
if(isset($_GET['cpf'])){
$sql = "select cpf, nome, cep from pessoas
where cpf=" .$_GET['cpf'];
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_assoc($result) ;
$cpf = $row['cpf'];
echo $cpf;
}
}

?>





ERROR:

Warning: mysqli_num_rows() expects parameter 1 to be
mysqli_result, boolean given in
C:\xampp\htdocs\Banco_de_dados\edit.php on line 158


Connect.php:


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "meubd";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT nome, cpf, cep, rua, bairro, cidade, estado, ibge FROM pessoas";
$result = $con->query($sql);
?>




Answer

You need to write quotes around the statement of the GET.

$sql = "select cpf, nome, cep from pessoas where cpf='" . $_GET['cpf'] . "'";
Comments