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

那年仲夏 提交于 2019-12-29 02:11:46

问题


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

        $count=mysql_num_rows($result);

回答1:


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

mysql_query() or die(mysql_error());

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




回答2:


Warning: mysql_ finctions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

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

Try

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



回答3:


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!



来源:https://stackoverflow.com/questions/18306038/warning-mysql-num-rows-expects-parameter-1-to-be-resource-boolean-given-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!