简体   繁体   English

在WHERE子句中使用变量时,PHP MySQL结果出现问题

[英]PHP MySQL result issue when using a variable in the WHERE Clause

I am having a pretty weird problem with my PHP MySQL Query, I am trying to return rows that have the correct rname, rcity, and rstate values. 我的PHP MySQL查询存在一个很奇怪的问题,我试图返回具有正确rname,rcity和rstate值的行。

$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate . "'";

When I run that query, it only returns 0 results. 当我运行该查询时,它仅返回0个结果。 However after some playing around with it, If I only use rname and rstate in the WHERE Clause it returns results. 但是,经过一番尝试之后,如果我仅在WHERE子句中使用rname和rstate,它将返回结果。

$sql = "SELECT * FROM `images` WHERE rname = '" . $rname . "' AND rstate = '" . $rstate . "';

That works perfect. 完美的作品。 So when i tried just useing rcity in the WHERE Clause. 因此,当我尝试仅在WHERE子句中使用rcity时。

$sql = "SELECT * FROM `images` WHERE rcity = '" . $rcity . "'";

0 results return. 返回0个结果。 So something is wrong with the rcity portion of the query. 因此查询的rcity部分出了问题。 If I hard write the value into the query instead of the $rcity variable, it pulls up results. 如果我将值而不是$ rcity变量硬写到查询中,则会拉出结果。 I doubled checked to make sure $rcity was declared, it has the correct value, etc. 我仔细检查了一下,以确保声明了$ rcity,它具有正确的值,等等。

I also created another test table in the database to check to see if it was a problem on the db side. 我还在数据库中创建了另一个测试表,以检查它在数据库方面是否存在问题。 Which the problem still existed. 哪个问题仍然存在。

Here is the full code of the getQuery() Function 这是getQuery()函数的完整代码

private function getQuery($data){
    // Takes raw data and creats image(s) query to search for listing resort...
        $listing = $data['listing'];
        $rname = $data['rname'];
        $rcity = $data['rcity'];
        $rstate = $data['rstate'];

        $query = "SELECT * FROM `test` WHERE rname = '" . $rname . "' AND rcity = '" . $rcity . "' AND rstate = '" . $rstate ."'";

        return $query;
}

And Here is my database class 这是我的数据库类

class db {

        public function __construct(){
            $this->server = DB_SERVER;
            $this->user = DB_USER;
            $this->Pass = DB_PASS;
            $this->Database = DB_Database;
        }

        protected function connect(){
            return mysqli_connect($this->server, $this->user, $this->Pass, $this->Database);
        }

        public function query($sql){

            $conn = $this->connect();
            $query = $conn->query($sql);

                if($query == false) {
                    throw new Exception("Query failed:".PHP_EOL.$conn->error.PHP_EOL.$sql);
                }
                if($query->num_rows == 0) {
                    // need E_NOTICE errors enabled to see this,
                    // on screen if display_errors is on, else in PHP error log
                    trigger_error("Query returned 0 rows:".PHP_EOL.$sql);
                }
                $result = array();
                    while ($row = $query->fetch_assoc()){
                        $result[] = $row;
                    }
                return $result;
        }

    }

I call the query in a class __construct function like so 我像这样在类__construct函数中调用查询

$con = new db;

    $sql = $this->getQuery($data);
    $result = $con->query($sql);

I think problem can be with syntax or mysql screening. 我认为问题可能出在语法或mysql筛选上。 Try to use PDO with bindParam method 尝试将PDO与bindParam方法一起使用

$sql = "SELECT * FROM `images` WHERE rname = '$rname' AND rcity = '$rcity' AND rstate = '$rstate'";

尝试实现这一点,即直接在' (撇号)之间使用变量,它应该可以完美地工作

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

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