簡體   English   中英

PHP的-MySQli的行數總是返回0?

[英]php - MySQli row count always returning 0?

rowLogin始終返回0 但是當我在sql上運行查詢時,我得到正確的結果! 我看過很多解決方案,但實際上沒有一個工作。

$queryLogin =("SELECT count(*) as count FROM `tblUser` WHERE `username` = '$username' and `password` ='$password'");
$resultLogin = $mysqli->query($queryLogin);
$rowLogin = $resultLogin->fetch_assoc();

echo $rowLogin['count']." rows in table tblUser.";

您的代碼不是很好...

您確實需要使用帶有用戶名/通過檢查的MYSQLi准備的語句esp ...

$mysqli = new mysqli(...);
$query = "SELECT count(*) as count FROM `tblUser` WHERE `username`=? and `password`=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss',$username,$password);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows){
    $row = $result->fetch_assoc();
    echo $row['count'] . " rows in table tblUser.";
}

我了解您可能只是在獲取一些信息,但這是采取此方法的最佳途徑。 不要繼續前進。

您的查詢很可能失敗,因為您的實際查詢中包含( ) 因此,如果要使用它,只需使用$queryLogin行上的( )

 $queryLogin =("SELECT count(*) as count FROM tblUser WHERE username = '".$username."' and password ='".$password."' "); $resultLogin = $mysqli->query($queryLogin); $rowLogin = $resultLogin->fetch_assoc(); echo $rowLogin['count']." rows in table tblUser."; or printf("Select returned %d rows.\\n", mysqli_num_rows($resultLogin)); 

您不需要范圍。

$queryLogin = "SELECT count(*) as count FROM `tblUser` WHERE `username` = '$username' and `password` ='$password'";
$resultLogin = $mysqli->query($queryLogin);
$rowLogin = $resultLogin->fetch_assoc();

echo $rowLogin['count']." rows in table tblUser.";

僅在內聯查詢中使用范圍。 您已經在這里使用范圍...

$resultLogin = $mysqli->query($queryLogin);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM