1

我的mysqli_fetch_array()代码有问题。我已经多次交换和更改变量以弄清楚为什么它不起作用。本质上,我需要它来访问我的数据库并找到符合特定条件的行数并返回值。关于我可以做些什么来让我的mysqli_fetch_array()功能发挥作用的任何见解?

谢谢。

<html>
<?php
$uname = "";
$pword = "";
$errorMessage = "";

function quote_smart($handle, $value) {
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }

    if (!is_numeric($value)) {
        $value = "'" . mysqli_real_escape_string($handle, $value) . "'";
    }

    return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$uname = $_POST['username'];
$pword = $_POST['password'];

$uname = htmlspecialchars($uname);
$pword = htmlspecialchars($pword);

$con = mysqli_connect("127.0.0.1", "root", "");
$db = mysqli_select_db($con, "users");

if ($db) {

    $uname = quote_smart($con, $uname);
    $pword = quote_smart($con, $pword);

    $SQL = "SELECT * FROM users WHERE L1 = $uname AND L2 = md5($pword)";
    $result = mysqli_query($con, $SQL);
    $num_rows = mysqli_fetch_array($result,MYSQLI_NUM);

    if ($result) {
        if ($num_rows > 0) {
            session_start();
            $_SESSION['login'] = "1";
            header ("Location: home.php");
        }
        else {
            session_start();
            $_SESSION['login'] = "";
            header ("Location: regpage.php");
        }   
    }
    else {
        $errorMessage = "Error logging on";
    }

mysqli_close($con);

}

else {
    $errorMessage = "Error logging on";
}

}
?>
</html>

我遇到的主要代码是:

   $num_rows = mysqli_fetch_array($result,MYSQLI_NUM);

每次运行它时,我都会收到以下错误消息:

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值

4

3 回答 3

0

尝试

$SQL = "SELECT * FROM users WHERE L1 = '".$uname."' AND L2 = '".md5($pword)."'";
$result = mysqli_query($con, $SQL)or die(mysqli_error($connection));
$num_rows = mysqli_num_rows($result);

使用 mysqli_error 并在其中传递连接作为参数

$connection = mysqli_connect("localhost", "root", "", "stackoverflow");
$result = mysqli_query($connection, $SQL)or die(mysqli_error($connection));

测试代码

<?php
    $uname = "";
    $pword = "";
    $errorMessage = "";

    function quote_smart($handle, $value)
    {
        if (get_magic_quotes_gpc())
        {
            $value = stripslashes($value);
        }

        if (!is_numeric($value))
        {
            $value = "'" . mysqli_real_escape_string($handle, $value) . "'";
        }
        return $value;
    }

    if (isset($_REQUEST['submit']))
    {
        $uname = $_POST['username'];
        $pword = $_POST['password'];

        $uname = htmlspecialchars($uname);
        $pword = htmlspecialchars($pword);

        $con = mysqli_connect("localhost", "root", "","stackoverflow");

        if ($con)
        {
            $uname = quote_smart($con, $uname);
            $pword = quote_smart($con, $pword);

            $SQL = "SELECT * FROM users WHERE username = ".$uname." AND password = ".$pword."";

            $result = mysqli_query($con, $SQL);
            $num_rows = mysqli_fetch_array($result,MYSQLI_NUM);

            if ($result)
            {
                if ($num_rows > 0)
                {
                    session_start();
                    $_SESSION['login'] = "1";
                    header ("Location: home.php");
                    exit;
                }
                else
                {
                    session_start();
                    $_SESSION['login'] = "";
                    header ("Location: regpage.php");
                    exit;
                }   
            }
            else
            {
                $errorMessage = "Error logging on";
            }
            mysqli_close($con);
        }
        else
        {
            $errorMessage = "Error logging on";
        }
    }
?>
于 2013-02-28T04:55:32.870 回答
0

$num_rows = mysqli_fetch_array($result,MYSQLI_NUM);这导致数据数组而不是行数

获取使用的行数mysqli_num_rows($result)

或者

$num_rows = count(mysqli_fetch_array($result,MYSQLI_NUM));

于 2013-02-28T04:55:37.927 回答
0

md5()php函数而不是mysql函数。在您的情况下,您使用它就像 mysql 函数一样。只需将您的查询替换为:

$SQL = "SELECT * FROM users WHERE L1 = $uname AND L2 = '".md5($pword)."' ";
于 2013-02-28T05:02:37.487 回答