Skip navigation links

MySQL Forums :: PHP :: PHP OO query problem --Couldn't fetch my_mysqli


Advanced Search

PHP OO query problem --Couldn't fetch my_mysqli
Posted by: Roy Leroux ()
Date: November 30, 2013 02:16PM

I'm having trouble getting an OO version of a PHP program to query a mysql database.
I can do it with procedure code but it dies using an OO class.
-----------------------------------------
Here's the output:

Try OO
at _construct:Success... localhost via TCP/IP
at do_query:($this=my_mysqli)
Warning: mysqli::query(): Couldn't fetch my_mysqli in C:\Websites\Temp\OO\Try.php on line 30
do_query,show tables, error=
Warning: my_mysqli::do_query(): Couldn't fetch my_mysqli in C:\Websites\Temp\OO\Try.php on line 31

at do_close:close failed
Try procedural
at open_db:Success... localhost via TCP/IP
at doquery:Query succeeded result=classes, items, packedins,
at doclose:doclose succeeded
---------------------------------
here's the code: first the non working OOP then the Procedural which works.
What am I doing wrong? (These are fake user and pwd.)

<?php
echo "
<!DOCTYPE HTML>
<html><head><title>Database Test</title>
<meta charset=\"UTF-8\">
<style>h3,h4 {margin:0 0 0 0}</style>
</head>
<body>
";
//-----------------------------OOP-----------
echo "<h4>Try OO</h4>";
$DB=new my_mysqli('travel');
$DB->do_query('show tables');
$DB->do_close();

class my_mysqli extends mysqli {
public function __construct($database){
echo "at _construct:";
$database="";
$server='localhost';
$username='myuser';
$userpassword='mypassword';
$link = mysqli_connect($server,$username,$userpassword,$database);
if (!$link) die('Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
else echo 'Success... ' . mysqli_get_host_info($link) . "<br />\n";
}//end __construct

public function do_query($query){
echo "at do_query:(\$this=".get_class($this).")";
$result=$this->query($query); //line 62
if (!$result){echo "do_query,$query, error="; echo $this->error; echo "<br />";} //line 63
else {echo "Query succeeded result=";
while ($row = $result::fetch_assoc($result)) {
foreach($row as $value){echo "$value, ";}
}//end while
echo "<br />\n";
}//end else
}//end do_query

public function do_close(){
echo "at do_close:";
if (!$this->close) echo "close failed<br />\n"; else echo "close succeeded<br />\n";
}//end close
}//end class database

//-------------------------procedural---------
echo "<h4>Try procedural</h4>";
$DB2=open_db('travel');
doquery($DB2,'show tables');
doclose($DB2);

function open_db($database){
echo "at open_db:";
$server="localhost";
$username="mysqlartsuser";
$userpassword="MYSQL[User]1";
$link = mysqli_connect($server,$username,$userpassword,$database);
if (!$link) die('Connect Error ('.mysqli_connect_errno() . ')'.mysqli_connect_error());
else echo 'Success... ' . mysqli_get_host_info($link) . "<br />\n";
return $link;
}//end open_db

function doquery($link,$query){
echo "at doquery:";
$result=mysqli_query($link,$query);
if (!$result) {echo "$Query failed". $link->error."<br />";}
else{ echo "Query succeeded result=";
while ($row = mysqli_fetch_assoc($result)) {
foreach($row as $value) echo "$value, ";
}//end while
echo "<br />\n";
}//end if else
}//end doquery

function doclose($link){
echo "at doclose:";
if (mysqli_close ($link)) echo "doclose succeeded<br />"; else echo "close failed<br />";
}//end doclose

?>



Edited 2 time(s). Last edit at 12/02/2013 10:19AM by Roy Leroux.

Options: ReplyQuote


Subject Written By Posted
PHP OO query problem --Couldn't fetch my_mysqli Roy Leroux 11/30/2013 02:16PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Rick James 12/02/2013 04:16PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Roy Leroux 12/02/2013 04:56PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Rick James 12/04/2013 04:15PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Roy Leroux 12/05/2013 11:09AM
Re: PHP OO query problem --Couldn't fetch my_mysqli Rick James 12/07/2013 01:30PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Roy Leroux 12/07/2013 07:47PM
Re: PHP OO query problem --Couldn't fetch my_mysqli Rick James 12/09/2013 02:47PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.