简体   繁体   中英

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.

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(); inside 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.

Without a WHERE clause, there will likely be multiple rows. The first result row is consumed and goes into the aether by the Fetch() inside select() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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