Click here to Skip to main content
15,886,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried my best to find out the result on my own, but have failed.

Here's the source code I've tried so far:

PHP
<?php

class DataBase
{
    private $connect;
    private $dbUser;
    private $dbHost;
    private $dbPassword;
    private $dbDatabase;
    private $numRows;
    private $results;

    public function connect($host, $username, $pass, $db)
    {
        $this->dbHost = $host;
        $this->dbUser = $username;
        $this->dbPassword = $pass;
        $this->dbDatabase = $db;

        return $this->connect = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbDatabase);
    }

    public function disConnect($connect) {
        return mysqli_close($this->connect = $connect);
    }

public function select($table, $where = array(), $orderBy = NULL) {
        if (count($where) === 3) {
            $operators = array('=', '<', '>', '>=', '<=', 'LIKE');

            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];

            if (in_array($operator, $operators)) {
                $query = "SELECT * FROM {$table} WHERE '". $field . $operator . $value . "'";
                
            }

            $this->results = mysqli_query($this->connect, $query);

            if ($this->results) {
                $this->numRows = mysqli_num_rows($this->results);
                return $this->numRows;
            } else {
                die(mysqli_error($this->connect));
            }
        }
    }

    public function countRows()
    {
        return $this->numRows == 0 ? false : true;
    }
}



Here's where and how I call the methods:

PHP
$suc = "";
$name = "";
if (isset($_POST['btnSubmit'])) {
    $con = new DataBase();
    $objDb = $con->connect('localhost', 'root', '', 'practice');

    $username = $_POST["username"];

    $suc = $con->select('users', ['username', 'LIKE', '%'.$username.'%']);

    if ($suc > 0) {
        print_r($con->countRows());
    } else {
        echo "Unable to Find Record !";
    }
    //print_r($suc);
    echo $name;

    $con->disConnect($objDb);
}
?>


Am getting error as Unable to Find Record !

Kindly solve my query.

Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900