错误“mysqli_num_rows”

时间:2016-05-09 11:04:17

标签: php html mysqli

如何解决此错误?

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;
	}
   }
	
?>

错误:

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);
?>

2 个答案:

答案 0 :(得分:0)

您需要在GET声明周围写下引号。

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

答案 1 :(得分:0)

使用以下代码     

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

        }
    }
    ?>

不要指定mysqli_num_rows($ result);直。检查结果是否有值。