简体   繁体   English

where子句变量发生故障

[英]where clause variable malfunctioning

I am new to oop environment and there is something that seems to be not right here, I have searched all over but the ones available is the query string is given inside the class. 我是oop环境的新手,这里似乎不正确,我已经搜索了所有内容,但可用的是在类内部提供了查询字符串。

if(isset($_POST['search'])){
    include('../Classes/class.config.php');
    include('../Classes/class.dbActions.php');
    $execute=new execute();
    $execute->select('SELECT * FROM users where username="'.$_POST['search'].'" ');
    $array=$execute->Fetch();
    echo $array['chasisNo'].'<br/>';
}

when I change: 当我改变时:

$execute->select('SELECT * FROM users where username="'.$_POST['search'].'" ');

to this 对此

$execute->select('SELECT * FROM users '); 

It works perfectly by listing the top 通过列出顶部它可以完美地工作
Now what's wrong with this code in that even if i pass a string like 现在,即使我传递了像

$execute->select('SELECT * FROM users where username="oketch" ');

it does not work 这是行不通的

here is the execute class 这是执行类

class execute extends db
{
    private $sqlString;
    private $selected;
    private $querySelected;
    public function select($sqlString)
    {
        $connection=$this->getConnection();
        $this->querySelected=$connection->query($sqlString);
        if($this->querySelected->num_rows>0) {
            $this->Fetch();
        }
        else {
            echo'No Data Available';
        }   
    }   

    public function Fetch() {
        return $this->querySelected->fetch_array();
    }

You're doing a $this->Fetch(); 您正在执行$this->Fetch(); inside select() . select()内部。 I assume this fetches one row of the result set. 我假设这将获取结果集的一行。 You then do a subsequent $execute->Fetch() to actually get the result you're looking for. 然后,您执行后续的$execute->Fetch()来实际获得所需的结果。

Without a WHERE clause, there will likely be multiple rows. 如果没有WHERE子句,则可能会有多行。 The first result row is consumed and goes into the aether by the Fetch() inside select() . 第一个结果行被select()内的Fetch()消耗并以太坊。 You then retrieve the second row and display it. 然后,您检索第二行并显示它。
With a WHERE clause there's only one row which is consumed into the aether, and then there's no actual row left to fetch and display. 使用WHERE子句,只有一行被消耗到以太中,然后就没有实际的行要提取和显示了。

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

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