Note that there are some explanatory texts on larger screens.

plurals
  1. POExtending a class from the class which is extended from mysqli
    primarykey
    data
    text
    <p>I am not sure if this is possible as I am not very good in OOP programming yet.</p> <p>I have this db class extended from mysqli,</p> <pre><code>class database extends mysqli { # overwrite parent __construct public function __construct($hostname = null,$username = null,$password = null,$database = null,$port = null, $socket = null) { $hostname = $hostname !== null ? $hostname : ini_get("mysqli.default_host"); $username = $username !== null ? $username : ini_get("mysqli.default_user"); $password = $password !== null ? $password : ini_get("mysqli.default_pw"); $database = $database !== null ? $database : ""; $port = $port !== null ? $port : ini_get("mysqli.default_port"); $socket = $socket !== null ? $socket : ini_get("mysqli.default_socket"); parent::__construct($hostname,$username,$password,$database,$port,$socket); # check if connect errno is set if (mysqli_connect_errno()) { throw new RuntimeException('Cannot access database: ' . mysqli_connect_error()); } } # fetches all result rows as an associative array, a numeric array, or both # mysqli_fetch_all (PHP 5 &gt;= 5.3.0) public function fetch_all($query) { $result = parent::query($query); if($result) { # check if mysqli_fetch_all function exist or not if(function_exists('mysqli_fetch_all')) { # NOTE: this below always gets error on certain live server # Fatal error: Call to undefined method mysqli_result::fetch_all() in /.../class_database.php on line 28 return $result-&gt;fetch_all(MYSQLI_ASSOC); } # fall back to use while to loop through the result using fetch_assoc else { while($row = $result-&gt;fetch_assoc()) { $return_this[] = $row; } if (isset($return_this)) { return $return_this; } else { return false; } } } else { return self::get_error(); } } # fetch a result row as an associative array public function fetch_assoc($query) { $result = parent::query($query); if($result) { return $result-&gt;fetch_assoc(); } else { # call the get_error function return self::get_error(); # or: # return $this-&gt;get_error(); } } public function query($query) { $result = $this-&gt;query($query); if($result) { return $result; } else { return $this-&gt;get_error(); } } ... # display error public function get_error() { if($this-&gt;errno || $this-&gt;error) { return sprintf("Error (%d): %s",$this-&gt;errno,$this-&gt;error); } } public function __destruct() { parent::close(); //echo "Destructor Called"; } } </code></pre> <p>and I have this procedural style of code which I want to turn it into a class that extended from the database class above,</p> <pre><code>if(isset($_REQUEST['search'])) $search = $_REQUEST['search']; $sql = " SELECT * FROM root_pages WHERE root_pages.pg_cat_id = '2' AND root_pages.parent_id != root_pages.pg_id AND root_pages.pg_hide != '1' AND root_pages.pg_url != 'cms' AND root_pages.pg_content_1 LIKE '%".$search."%' OR root_pages.pg_content_2 LIKE '%".$search."%' AND root_pages.pg_content_1 NOT LIKE '%http://%' AND root_pages.pg_content_2 NOT LIKE '%http://%' ORDER BY root_pages.pg_created DESC "; $items = $connection-&gt;fetch_all($sql); $total_item = $connection-&gt;num_rows($sql); </code></pre> <p>so I think, by theory I can extend this code into a class like this,</p> <pre><code>class search extends database { public $search = null; public function __construct($keyword) { $this-&gt;search = $keyword; } public function get_result() { $sql = " SELECT* FROM root_pages WHERE root_pages.pg_cat_id = '2' AND root_pages.parent_id != root_pages.pg_id AND root_pages.pg_hide != '1' AND root_pages.pg_url != 'cms' AND root_pages.pg_content_1 LIKE '%".$this-&gt;search."%' OR root_pages.pg_content_2 LIKE '%".$this-&gt;search."%' AND root_pages.pg_content_1 NOT LIKE '%http://%' AND root_pages.pg_content_2 NOT LIKE '%http://%' ORDER BY root_pages.pg_created DESC "; $items = parent::fetch_all($sql); return $items; } } </code></pre> <p>then I call the object of search,</p> <pre><code>$output = new search('1'); print_r($output-&gt;get_result()); </code></pre> <p>but I get lots of errors instead,</p> <blockquote> <p>Warning: mysqli::query() [mysqli.query]: Couldn't fetch search in C:\wamp\www\xxx\class_database.php on line xx</p> <p>Warning: database::get_error() [database.get-error]: Couldn't fetch search in C:\wamp\www\xxx\class_database.php on line xx</p> <p>Warning: mysqli::close() [mysqli.close]: Couldn't fetch search in C:\wamp\www\xxx\class_database.php on line xx</p> </blockquote> <p>What have I done incorrectly? How can I fix it?</p> <p>Thanks.</p> <p><strong>EDIT:</strong></p> <p>When I tried to call the child class (search) from the parent class (database) in this way,</p> <pre><code>$database = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME); print_r(search::get_result()); </code></pre> <p>then I get this error,</p> <blockquote> <p>Fatal error: Non-static method mysqli::query() cannot be called statically in C:\wamp\www\xxx\class_database.php on line</p> </blockquote> <p>Sigh...</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload