使用mysqli查询查询失败

时间:2015-09-24 05:35:30

标签: php

显示查询的代码失败但无法理解为什么会发生这种情况。它只是用于测试代码。

<?php
require_once("database_function.php");

class Auth {
    public $password = "123456a";
    public $username = "allen";
    public function matching() {
        Global $Database;

        $sql = "SELECT * FROM auth";
        $sql .= " WHERE username =" . $this->{'username'};
        $sql .= " AND password =" . $this->{'password'};
        $sql .= " LIMIT 1";
        $result = mysqli_query($Database-> connect, $sql);
        if ($result) {
            echo "Querying Successfull";
        } elseif (!$result) {
            echo "Querying failed";
        }
    }
}

$authentication = new Auth();
$authentication -> matching();

?>

3 个答案:

答案 0 :(得分:1)

使用$username$password代替$this{'username'}$this{'password'}

<?php
    require_once ("database_function.php");

    class Auth {
        public $password = "123456a";
        public $username = "allen";
        public function matching () {
            Global $Database;
            $sql = "SELECT * FROM auth";
            $sql .= " WHERE username =".$username;
            $sql .= " AND password =".$password;
            $sql .= " LIMIT 1";
            $result = mysqli_query ($Database->connect, $sql);
            if ($result) {
                echo "Querying Successfull";
            } elseif (!$result) {
                echo "Querying failed";
            }
        }
    }

    $authentication = new Auth();
    $authentication -> matching();

?>

答案 1 :(得分:0)

此: SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(ImportForm.sqlConnection);

应该是这样的: $result = mysqli_query($Database-> connect, $sql);

&#34; $result = mysqli_query($Database->connect, $sql);&#34;之间没有空格和&#34;连接&#34;。

我在查询中排除第二个参数。小鲍比表。

答案 2 :(得分:0)

在字段值中使用单引号 $ sql。=“WHERE username ='”。 $ this-&gt; {'username'}。“'”; $ sql。=“AND password ='”。 $ this-&gt; {'password'}。“'”;