How do I create a user defined function for mysqli_num_rows in php OOPs? -
i have tried best find out result on own, have failed.
here's source code have tried @ level best.
<?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 * {$table} '". $field . $operator . $value . "'"; } if (mysqli_query($this->connect, $query)) { return true; } else { die(mysqli_error($this->connect)); } } } public function countrows($queryres) { if (mysqli_num_rows($queryres) > 0) { return true; } else { return false; } } }
i error:
warning: mysqli_num_rows() expects parameter 1 mysqli_result, boolean given in c:\wamp\www\practice\index.php on line 70
and line 70 is: if (mysqli_num_rows($queryres) > 0) {
here's , how call method:
$suc = ""; 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) { print_r($con->countrows($suc)); } else { echo "unable find record !"; } //print_r($suc); $con->disconnect($objdb); } ?>
kindly guide me making mistake.
thanks
function countrows($sql) { $rst = @mysql_query($sql)or trigger_error("sqxxxl", e_user_error); return $numrows = mysql_num_rows($rst); }
Comments
Post a Comment