![](/img/trans.png)
[英]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource with no mysql_error()
[英]Help I have an error message using PHP “Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in…”
我正在嘗試在 MySql 數據庫中運行查詢,但每次都收到相同的警告。 有人可以幫忙嗎。
警告:mysql_num_rows():在第 279 行提供的參數不是有效的 MySQL 結果資源/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/public_html/login.php 您提供的用戶名不存在!
我用 ++++++++++++$num = mysql_num_rows($res);++++++++++++ 突出顯示了第 279 行
請讓我知道如何解決這個問題。
<?php
//If the user has submitted the form
if($_POST['submit']) {
//protect the posted value then store them to variables
$username = protect($_POST['username']);
$password = protect($_POST['password']);
//Check if the username or password boxes were not filled in
if(!$username || !$password) {
//if not display an error message
echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
} else {
//if the were continue checking
//select all rows from the table where the username matches the one entered by the user
$res = mysql_query("SELECT *
FROM `users`
WHERE `username` = '".$username."'");
++++++++++++++$num = mysql_num_rows($res);++++++++++++++++++
//check if there was not a match
if($num == 0) {
//if not display an error message
echo "<center>The <b>Username</b> you supplied does not exist!</center>";
} else {
//if there was a match continue checking
//select all rows where the username and password match the ones submitted by the user
$res = mysql_query("SELECT * FROM `users`
WHERE `username` = '".$username."'
AND `password` = '".$password."'");
$num = mysql_num_rows($res);
//check if there was not a match
if($num == 0) {
//if not display error message
echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
} else {
//if there was continue checking
//split all fields fom the correct row into an associative array
$row = mysql_fetch_assoc($res);
//check to see if the user has not activated their account yet
if($row['active'] != 1){
//if not display error message
echo "<center>You have not yet <b>Activated</b> your account!</center>";
}else{
//if they have log them in
//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['uid'] = $row['id'];
//show message
echo "<center>You have successfully logged in!</center>";
//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
//redirect them to the usersonline page
header('Location: usersOnline.php');
}
}
}
}
}
?>
我不確定保護 function 正在做什么,但您可以執行以下操作:
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
並在您的查詢中,用於調試使用它來獲得正確的錯誤
$res = mysql_query("SELECT *
FROM `users`
WHERE
`username` = '$username'
AND
`password` = '$password'") or die('Error: '.mysql_error());
$username 可能包含特殊字符嗎? 如果您的查詢從數據庫的角度正確執行,您應該檢查 mysql 日志。
可能您的查詢失敗。 如果發生錯誤, mysql_query()返回false
。 使用mysql_error()來確定真正的問題。
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
if ($res === false) {
echo mysql_errno() . ': ' . mysql_error();
exit;
}
$num = mysql_num_rows($res);
正如 mysql_query() 的文檔所述:
對於 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回結果集的語句,mysql_query() 成功時返回資源,錯誤時返回 FALSE。
因此,只需檢查您在連接的數據庫中是否有此表、列出的列以及您的查詢是否正常 - 這可能只是您的查詢錯誤。
文檔中有示例(請參閱開頭的鏈接),如何查看實際發生的情況(請參閱示例 #1;替換為您自己的查詢):
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.