简体   繁体   English

MYSQLI_NUM_ROWS始终返回0

[英]MYSQLI_NUM_ROWS ALWAYS RETURNING 0

i'm trying to get rid of a bug ASAP. 我试图尽快摆脱一个错误。 I'm using mysql_num_rows but it ALWAYS returns 0. And i dont know if it's because i have the wrong syntax or what... can you guys help me? 我正在使用mysql_num_rows,但是它总是返回0。而且我不知道这是因为我的语法错误还是什么……你们可以帮我吗? here's the code; 这是代码;

<?php
include_once("checklogin.php");
$u = "";
if(isset($_GET["u"])){
    $u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
} else {
    header("location: http://www.myswesite.com/login.php");
    exit();    
}
$sql = "SELECT * FROM users WHERE username='$u' AND activated='1' LIMIT 1";
$user_query = mysqli_query($connection, $sql);
$numrows = mysqli_num_rows($user_query);
if($numrows < 1){
    echo "User does not exist or is not yet activated, press back";
    exit(); 
}
?>

You have to use store_result to buffer the result set before you can get the num rows. 必须先使用store_result缓冲结果集,然后才能获取num行。

$user_query = mysqli_query($connection, $sql);
mysqli_store_result($connection);
$numrows = mysqli_num_rows($user_query);

See http://www.php.net/manual/en/mysqli-result.num-rows.php : 参见http://www.php.net/manual/en/mysqli-result.num-rows.php

For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved. 对于无缓冲的结果集,在检索到结果中的所有行之前,mysqli_num_rows()不会返回正确的行数。

i think there is a syntax error in your query when you are passing username try this i think it works for you. 我认为当您传递用户名时,查询中存在语法错误,请尝试使用此方法,我认为它对您有用。

$username=$_SESSION['username'];
   SELECT * FROM tbl_users WHERE Username='".$username."'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM