-2

我该如何解决这个问题?

警告:mysql_num_rows():提供的参数不是第 114 行 /home/u940004575/public_html/index.php 中的有效 MySQL 结果资源

我已经更改并摆弄了这段代码太久了,请帮忙

    <?
    mysql_connect("XXXXX","XXXXX","XXXXX") or    die(mysql_error());
    mysql_select_db("u940004575_chat");

    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $sha1mypassword = sha1($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sha1mypassword = mysql_real_escape_string($sha1mypassword);
    $sql=mysql_query("SELECT id FROM users WHERE username='$myusername' and password='$sha1mypassword'")or die(mysql_error());
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);
    if($count==1){
    session_register("myusername");
    session_register("mypassword"); 
    header("location:home.php");
    }
    else {
    echo "Wrong Username or Password";
    }

    ?>
4

2 回答 2

1

使用if条件来避免此错误

尝试这个

 <?php
    mysql_connect("XXXXX","XXXXX","XXXXX") or    die(mysql_error());
    mysql_select_db("u940004575_chat");

    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $sha1mypassword = sha1($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sha1mypassword = mysql_real_escape_string($sha1mypassword);
    $sql=mysql_query("SELECT id FROM users WHERE username='".$myusername."' and password='".$sha1mypassword."'")or die(mysql_error());


    if($sql){
$count=mysql_num_rows($sql);
}
    if($count==1){
    session_register("myusername");
    session_register("mypassword"); 
    header("location:home.php");
    }
    else {
    echo "Wrong Username or Password";
    }

    ?>

使用<?php代替<?

于 2013-09-26T11:05:38.733 回答
1

在你的代码中改变它:

$sql="SELECT id FROM users WHERE username='$myusername' and password='$sha1mypassword'";
$result=mysql_query($sql);

mysql_query在这两个地方都使用。

mysql_已弃用您的信息。

于 2013-09-26T11:06:02.580 回答