简体   繁体   English

mysql_num_rows没有返回任何行

[英]mysql_num_rows isn't returning any rows

So, I have some code. 所以,我有一些代码。 I know that mysql_num_rows is deprecated, but since I've already used it I don't want to switch everything to mysqli_. 我知道mysql_num_rows已被弃用,但由于我已经使用过它,我不想将所有内容都切换到mysqli_。 Anyway it was working on my local server and returning 1 or more results based on the entry. 无论如何,它在我的本地服务器上工作,并根据条目返回一个或多个结果。 This is a PHP login script that I'm trying to get to work. 这是我正在努力工作的PHP登录脚本。 When I uploaded the script to my hostgator server it didn't work. 当我将脚本上传到hostgator服务器时,它无法正常工作。 I also checked the PHP version and it mysql_num_rows() shouldn't be deprecated in version 5.4.xxx. 我还检查了PHP版本,并且不应在版本5.4.xxx中弃用mysql_num_rows()。

When I try doing a test query of just SELECT * FROM customers it returns one row, but it's not returning anything when I search for where the user and password equal the posted variables. 当我尝试仅对SELECT * FROM客户进行测试查询时,它返回一行,但当我搜索用户和密码等于发布变量的位置时,它不会返回任何内容。 It's frustrating me, and I could use a second set of eyes to look at this. 令我感到沮丧的是,我可以使用第二组眼睛看这个。

<?php
include('mysql_connect.php');
if(isset($_POST['submit'])) {
  if(isset($_POST['cususername']) AND isset($_POST['cuspassword'])) {
    $username = $_POST['cususername'];
    $password = md5($_POST['cuspassword']);
        $query = "SELECT * FROM customers WHERE username = '" . $username . "' AND 
        password = '" . $password . "'";
        $returned_user = mysql_query($query);
        $number_of_users = mysql_num_rows($returned_user);
        if($number_of_users > 0){
            echo "It got this far!";
            $customer_array = mysql_fetch_array($returned_user);
            $_SESSION['user_logged'] = 1;
            $_SESSION['id'] = $customer_array['customer_id'];
            $_SESSION['user_name'] = $customer_array['username'];
        }
  } 
}

?>
<?php 
if(isset($_REQUEST['loggoff'])) {
  unset($_SESSION['user_logged']);
  unset($logged_status);
}

if(isset($_SESSION['user_logged'])) { 
    $logged_status = $_SESSION['user_logged']; 
}

if(isset($logged_status)) {
  if($logged_status == 1) {
      echo "You are logged in as " . $_SESSION['user_name'] . ", Click here to <a href='" . $_SERVER['PHP_SELF'] . "?loggoff=1'>Log off</a>" . "<br>";
  }
}
else {?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="customerlogin">
    <input type="text" name="cususername" id="cususername" />
    <input type="password" name="cuspassword" id="cuspassword" />
    <br />
    <input type="submit" name="submit" value="Login" />
</form>
<?php}?>

You should check if you have an mysql_error in your query. 您应该检查查询中是否有mysql_error。 Btw. 顺便说一句。 you should mysql_real_escape your POST data before use in a mysql_query 你应该在使用mysql_query之前mysql_real_escape你的POST数据

If you would use columns names as array index you must use mysql_fetch_assoc() instead of mysql_fetch_array(); 如果要使用列名作为数组索引,则必须使用mysql_fetch_assoc()而不是mysql_fetch_array();

Try this Code: 试试这个代码:

<?php
include('mysql_connect.php');
if( isset( $_POST['submit'] ) )
{

    if( isset( $_POST['cususername'] ) AND
        isset( $_POST['cuspassword'] ) )
    {
        // Prevent mysql injection
        $username = mysql_real_escape_string( $_POST['cususername'] );
        $password = md5( $_POST['cuspassword'] );

        $query = "SELECT * FROM `customers` WHERE `username` = '" . $username . "' AND `password` = '". $password ."'";

        $result = mysql_query( $query );

        // Check if error
        if( mysql_errno() !== NULL )
        {
            exit(   "An mysql_error has occured: \r\n".
                    "Err-No: " . mysql_errno() . "\r\n".
                    "Err-Msg: " . mysql_error()
                );
        }

        // Everything is okay .. Go on
        $iRows = mysql_num_rows( $result );

        // Check if only one user was found
        if( $iRows === 1 )
        {
            echo "It got this far!";

            $customer = mysql_fetch_assoc( $result );

            // Use boolean value instead of numbers
            $_SESSION['user_logged']    = true;
            $_SESSION['id']             = $customer['customer_id'];
            $_SESSION['user_name']      = $customer['username'];
        }
    }
}

I hope I could help you. 我希望我能帮助你。

If this code wouldn't work try to output the generated sql statment and try it self in your SQL Admintool (eg phpMyAdmin). 如果此代码不起作用,请尝试输出生成的sql语句并在SQL Admintool中自行尝试(例如phpMyAdmin)。

Sorry for my bad english :/ 对不起,我的英语不好 :/

One Suggestion. 一个建议。 Rather Than 'AND'. 而不是'和'。 Use '&&'. 采用 '&&'。 Some Codes do not execute using AND in if conditon. 如果条件允许,某些代码不会使用AND执行。 Just a suggestion. 只是一个建议。

if(isset($_POST['cususername']) && isset($_POST['cuspassword']))

Ok guys, and for all those with a similar problem searching. 好的,对于那些有类似问题搜索的人。 Here is my answer. 这是我的答案。 The password field wasn't matching up with the inputted $_POST . 密码字段与输入的$_POST不匹配。 So, I got online and looked up an answer. 所以,我上网查了一个答案。 Come to find out the password was varchar(25) , which was cutting off the md5 hash. 来找出密码是varchar(25) ,这是切断md5哈希。 This was what was causing MySQL_NUM_ROWS() to return 0. Don't worry I will add MySQL injection prevention to my script as suggested by few of you, including Chris Wittor. 这就是导致MySQL_NUM_ROWS()返回0的原因。不要担心我会按照你们这些人的建议添加MySQL注入预防,包括Chris Wittor。 Also, I know that md5 is an outdated way of encypting the information, but it's better than plain text. 另外,我知道md5是一种过时的信息收集方式,但它比纯文本更好。

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

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