php - mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in "link to the file" -
i have error in component in joomla
that's code (the error in line 263):
<?php /** * @package joomla.platform * @subpackage database * * @copyright copyright (c) 2005 - 2013 open source matters, inc. rights reserved. * @license gnu general public license version 2 or later; see license */ defined('jpath_platform') or die; jloader::register('jdatabasemysql', dirname(__file__) . '/mysql.php'); jloader::register('jdatabasequerymysqli', dirname(__file__) . '/mysqliquery.php'); jloader::register('jdatabaseexportermysqli', dirname(__file__) . '/mysqliexporter.php'); jloader::register('jdatabaseimportermysqli', dirname(__file__) . '/mysqliimporter.php'); /** * mysqli database driver * * @package joomla.platform * @subpackage database * @see http://php.net/manual/en/book.mysqli.php * @since 11.1 */ class jdatabasemysqli extends jdatabasemysql { /** * name of database driver. * * @var string * @since 11.1 */ public $name = 'mysqli'; /** * constructor. * * @param array $options list of options used configure connection * * @since 11.1 */ protected function __construct($options) { // basic values options. $options['host'] = (isset($options['host'])) ? $options['host'] : 'localhost'; $options['user'] = (isset($options['user'])) ? $options['user'] : 'root'; $options['password'] = (isset($options['password'])) ? $options['password'] : ''; $options['database'] = (isset($options['database'])) ? $options['database'] : ''; $options['select'] = (isset($options['select'])) ? (bool) $options['select'] : true; $options['port'] = null; $options['socket'] = null; /* * unlike mysql_connect(), mysqli_connect() takes port , socket separate arguments. therefore, * have extract them host string. */ $tmp = substr(strstr($options['host'], ':'), 1); if (!empty($tmp)) { // port number or socket name if (is_numeric($tmp)) { $options['port'] = $tmp; } else { $options['socket'] = $tmp; } // extract host name $options['host'] = substr($options['host'], 0, strlen($options['host']) - (strlen($tmp) + 1)); // take care of following notation: ":3306" if ($options['host'] == '') { $options['host'] = 'localhost'; } } // make sure mysqli extension php installed , enabled. if (!function_exists('mysqli_connect')) { // legacy error handling switch based on jerror::$legacy switch. // @deprecated 12.1 if (jerror::$legacy) { $this->errornum = 1; $this->errormsg = jtext::_('jlib_database_error_adapter_mysqli'); return; } else { throw new jdatabaseexception(jtext::_('jlib_database_error_adapter_mysqli')); } } $this->connection = @mysqli_connect( $options['host'], $options['user'], $options['password'], null, $options['port'], $options['socket'] ); // attempt connect server. if (!$this->connection) { // legacy error handling switch based on jerror::$legacy switch. // @deprecated 12.1 if (jerror::$legacy) { $this->errornum = 2; $this->errormsg = jtext::_('jlib_database_error_connect_mysql'); return; } else { throw new jdatabaseexception(jtext::_('jlib_database_error_connect_mysql')); } } // finalize initialisation jdatabase::__construct($options); // set sql_mode non_strict mode mysqli_query($this->connection, "set @@session.sql_mode = '';"); // if auto-select enabled select given database. if ($options['select'] && !empty($options['database'])) { $this->select($options['database']); } } /** * destructor. * * @since 11.1 */ public function __destruct() { if (is_callable(array($this->connection, 'close'))) { mysqli_close($this->connection); } } /** * method escape string usage in sql statement. * * @param string $text string escaped. * @param boolean $extra optional parameter provide escaping. * * @return string escaped string. * * @since 11.1 */ public function escape($text, $extra = false) { $result = mysqli_real_escape_string($this->getconnection(), $text); if ($extra) { $result = addcslashes($result, '%_'); } return $result; } /** * test see if mysql connector available. * * @return boolean true on success, false otherwise. * * @since 11.1 */ public static function test() { return (function_exists('mysqli_connect')); } /** * determines if connection server active. * * @return boolean true if connected database engine. * * @since 11.1 */ public function connected() { if (is_object($this->connection)) { return mysqli_ping($this->connection); } return false; } /** * number of affected rows previous executed sql statement. * * @return integer number of affected rows. * * @since 11.1 */ public function getaffectedrows() { return mysqli_affected_rows($this->connection); } /** * gets exporter class object. * * @return jdatabaseexportermysqli exporter object. * * @since 11.1 * @throws jdatabaseexception */ public function getexporter() { // make sure have exporter class driver. if (!class_exists('jdatabaseexportermysqli')) { throw new jdatabaseexception(jtext::_('jlib_database_error_missing_exporter')); } $o = new jdatabaseexportermysqli; $o->setdbo($this); return $o; } /** * gets importer class object. * * @return jdatabaseimportermysqli importer object. * * @since 11.1 * @throws jdatabaseexception */ public function getimporter() { // make sure have importer class driver. if (!class_exists('jdatabaseimportermysqli')) { throw new jdatabaseexception(jtext::_('jlib_database_error_missing_importer')); } $o = new jdatabaseimportermysqli; $o->setdbo($this); return $o; } /** * number of returned rows previous executed sql statement. * * @param resource $cursor optional database cursor resource extract row count from. * * @return integer number of returned rows. * * @since 11.1 */ public function getnumrows($cursor = null) { return mysqli_num_rows($cursor ? $cursor : $this->cursor); } /** * current or query, or new jdatabasequery object. * * @param boolean $new false return last query set, true return new jdatabasequery object. * * @return mixed current value of internal sql variable or new jdatabasequery object. * * @since 11.1 * @throws jdatabaseexception */ public function getquery($new = false) { if ($new) { // make sure have query class driver. if (!class_exists('jdatabasequerymysqli')) { throw new jdatabaseexception(jtext::_('jlib_database_error_missing_query')); } return new jdatabasequerymysqli($this); } else { return $this->sql; } } /** * version of database connector. * * @return string database connector version. * * @since 11.1 */ public function getversion() { return mysqli_get_server_info($this->connection); } /** * determines if database engine supports utf-8 character encoding. * * @return boolean true if supported. * * @since 11.1 * @deprecated 12.1 */ public function hasutf() { jlog::add('jdatabasemysqli::hasutf() deprecated.', jlog::warning, 'deprecated'); return true; } /** * method auto-incremented value last insert statement. * * @return integer value of auto-increment field last inserted row. * * @since 11.1 */ public function insertid() { return mysqli_insert_id($this->connection); } /** * execute sql statement. * * @return mixed database cursor resource on success, boolean false on failure. * * @since 11.1 * @throws jdatabaseexception */ public function execute() { if (!is_object($this->connection)) { // legacy error handling switch based on jerror::$legacy switch. // @deprecated 12.1 if (jerror::$legacy) { if ($this->debug) { jerror::raiseerror(500, 'jdatabasemysqli::query: ' . $this->errornum . ' - ' . $this->errormsg); } return false; } else { jlog::add(jtext::sprintf('jlib_database_query_failed', $this->errornum, $this->errormsg), jlog::error, 'database'); throw new jdatabaseexception($this->errormsg, $this->errornum); } } // take local copy don't modify original query , cause issues later $sql = $this->replaceprefix((string) $this->sql); if ($this->limit > 0 || $this->offset > 0) { $sql .= ' limit ' . $this->offset . ', ' . $this->limit; } // if debugging enabled let's log query. if ($this->debug) { // increment query counter , add query object queue. $this->count++; $this->log[] = $sql; jlog::add($sql, jlog::debug, 'databasequery'); } // reset error values. $this->errornum = 0; $this->errormsg = ''; // execute query. $this->cursor = mysqli_query($this->connection, $sql); // if error occurred handle it. if (!$this->cursor) { $this->errornum = (int) mysqli_errno($this->connection); $this->errormsg = (string) mysqli_error($this->connection) . ' sql=' . $sql; // legacy error handling switch based on jerror::$legacy switch. // @deprecated 12.1 if (jerror::$legacy) { if ($this->debug) { jerror::raiseerror(500, 'jdatabasemysqli::query: ' . $this->errornum . ' - ' . $this->errormsg); } return false; } else { jlog::add(jtext::sprintf('jlib_database_query_failed', $this->errornum, $this->errormsg), jlog::error, 'databasequery'); throw new jdatabaseexception($this->errormsg, $this->errornum); } } return $this->cursor; } /** * select database use. * * @param string $database name of database select use. * * @return boolean true if database selected. * * @since 11.1 * @throws jdatabaseexception */ public function select($database) { if (!$database) { return false; } if (!mysqli_select_db($this->connection, $database)) { // legacy error handling switch based on jerror::$legacy switch. // @deprecated 12.1 if (jerror::$legacy) { $this->errornum = 3; $this->errormsg = jtext::_('jlib_database_error_database_connect'); return false; } else { throw new jdatabaseexception(jtext::_('jlib_database_error_database_connect')); } } return true; } /** * set connection use utf-8 character encoding. * * @return boolean true on success. * * @since 11.1 */ public function setutf() { mysqli_query($this->connection, "set names 'utf8'"); } /** * method fetch row result set cursor array. * * @param mixed $cursor optional result set cursor fetch row. * * @return mixed either next row result set or false if there no more rows. * * @since 11.1 */ protected function fetcharray($cursor = null) { return mysqli_fetch_row($cursor ? $cursor : $this->cursor); } /** * method fetch row result set cursor associative array. * * @param mixed $cursor optional result set cursor fetch row. * * @return mixed either next row result set or false if there no more rows. * * @since 11.1 */ protected function fetchassoc($cursor = null) { return mysqli_fetch_assoc($cursor ? $cursor : $this->cursor); } /** * method fetch row result set cursor object. * * @param mixed $cursor optional result set cursor fetch row. * @param string $class class name use returned row object. * * @return mixed either next row result set or false if there no more rows. * * @since 11.1 */ protected function fetchobject($cursor = null, $class = 'stdclass') { return mysqli_fetch_object($cursor ? $cursor : $this->cursor, $class); } /** * method free memory used result set. * * @param mixed $cursor optional result set cursor fetch row. * * @return void * * @since 11.1 */ protected function freeresult($cursor = null) { mysqli_free_result($cursor ? $cursor : $this->cursor); } /** * execute query batch. * * @param boolean $abortonerror abort on error. * @param boolean $transactionsafe transaction safe queries. * * @return mixed database resource if successful, false if not. * * @deprecated 12.1 * @since 11.1 */ public function querybatch($abortonerror = true, $transactionsafe = false) { // deprecation warning. jlog::add('jdatabasemysqli::querybatch() deprecated.', jlog::warning, 'deprecated'); $sql = $this->replaceprefix((string) $this->sql); $this->errornum = 0; $this->errormsg = ''; // if batch meant transaction safe need wrap in transaction. if ($transactionsafe) { $sql = 'start transaction;' . rtrim($sql, "; \t\r\n\0") . '; commit;'; } $queries = $this->splitsql($sql); $error = 0; foreach ($queries $query) { $query = trim($query); if ($query != '') { $this->cursor = mysqli_query($this->connection, $query); if ($this->debug) { $this->count++; $this->log[] = $query; } if (!$this->cursor) { $error = 1; $this->errornum .= mysqli_errno($this->connection) . ' '; $this->errormsg .= mysqli_error($this->connection) . " sql=$query <br />"; if ($abortonerror) { return $this->cursor; } } } } return $error ? false : true; } }
as link jon conde provided says, getting because have error in query. have not provided query it's impossible know problem is. unfortunately of advice getting on whole going make worse because problem not in jdatabasequerymysqli in code calling it. getting boolean false instead of expected results because query has failed.
to see generated query can use
echo $query->dump(); put before calling getnumrows(). may need die; depending on context or log or if in cms can turn on debugger , see generated queries (global configuration, debug on).
if provide code (not copy of api) people can debug query.
i it's bug it's not testing , throwing exception if query has failed.
Comments
Post a Comment