警告:mysql_num_rows() 期望参数 1 是资源,布尔值在第 18 行的 /home/folder/public_html/folder/folder/login.php 中给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18306038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:19:17  来源:igfitidea点击:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/folder/public_html/folder/folder/login.php on line 18

phpmysqldatabaseforum

提问by user2695012

I'm trying to set up my database however I ran into some issues sadly. I've seen that many people asked either the same or similar question but no matter how I look at my code, and modify it. I get confused more, and add more errors. So if someone could explain to me how I can attempt to fix it I'd appreciate it.

我正在尝试设置我的数据库,但是我很遗憾地遇到了一些问题。我已经看到很多人问过相同或类似的问题,但无论我如何查看我的代码并对其进行修改。我更困惑,并添加更多错误。因此,如果有人可以向我解释我如何尝试修复它,我将不胜感激。



Here is the error message:

这是错误消息:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/folder/public_html/folder/folder/login.php on line 18


Here is the code:

这是代码:

<link rel="Stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'>
<?php
$filename = 'install.php';
if (file_exists($filename)) {
echo ("<center><font color='red'><b>/install.php still exists<br>
After installing please delete install.php</center></font></b>");
} else {
if (isset($_POST['Login'])){
include('config.php');
    if (!mysql_connect($host, $username, $password)) die("Can't connect to database");
    if (!mysql_select_db($db_name)) die("Can't select database");
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    if($count >= 1){
    session_register("myusername");
    session_register("mypassword");
    header("location: index.php");
    } else {
        echo "<center><font color='red'><b>Wrong Username or Password</center></font></b>";
    }
}
?>
<br>
<form method="post" action=""><td>
<table width="325" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#212121">
<td><table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#404040"></td>
<tr colspan="3"><strong><center> <font color="ECECEC"> Admin Login </font></center></strong></tr>
<tr>
<td>
<font color="ECECEC">Username </font><input name="myusername" type="text" id="myusername">
<font color="ECECEC">Password </font><input name="mypassword" type="password" id="mypassword">
</td>
<center><td><input type="submit" name="Login" value="Login"></td></center>
</table></table>
</form>
<?php
}
?>


Here is line 18

这是第 18 行

        $count=mysql_num_rows($result);

回答by DevZer0

This error is because the previous call to mysql_queryis not producing a result set, but a falsestating that the query had failed.

这个错误是因为之前的调用mysql_query没有产生结果集,而是false声明查询失败。

mysql_query() or die(mysql_error());

to see the problem. try the above code. From the code you have posted $tbl_nameis not a visible variable name.

看到问题。试试上面的代码。从您发布的代码来看,$tbl_name不是可见的变量名称。

回答by bansi

Warning:mysql_ finctions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLior PDO_MySQLextension should be used.

警告:mysql_ 功能自 PHP 5.5.0 起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展。

You are not checking if the mysql_query was successful or not. If the query was unsuccessful mysql_query returns false. Refer to mysql_query Manual

您不是在检查 mysql_query 是否成功。如果查询不成功 mysql_query 返回 false。参考mysql_query 手册

Try

尝试

$result=mysql_query($sql);
if ($result==false)
{
    die(mysql_error());
}
$count=mysql_num_rows($result);

回答by Shareek Ahamed

I have gone through once...!

我经历过一次……!

Tried all possible ways.... didn't workout..!

尝试了所有可能的方法......没有锻炼......!

Finally found that there is nothing wrong in the code!! It's the problem when you "Import" a database where you have exported earlier!

终于发现代码没有问题!!当您“导入”之前导出的数据库时,问题就出现了!

(This will sound wired!!) but this was the case....!

(这听起来像是连线!!)但事实就是这样......!

Check your Exported DB Script, Whether it contains Create database code section.... with some other name or with a prefix!

检查您的导出数据库脚本,它是否包含创建数据库代码部分.... 使用其他名称或前缀!