Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 2.5.0b5

Bug #17377 MDB2 Error missing method
Submitted: 2010-05-07 17:08 UTC
From: shadoan Assigned: quipo
Status: Bogus Package: MDB2 (version 2.4.1)
PHP Version: 5.2.6 OS: Debian
Roadmaps: (Not assigned)    
Subscription  


 [2010-05-07 17:08 UTC] shadoan (Rene Wappenhans)
Description: ------------ Hi, I have created a script using smarty template section and I used the PEAR:DB package earlier for database connections and handling. Now I wanted to switch to MDB2 but I run on a problem with the SQL-Function, which I find no resolution for. Below is my code. The error message is: Fatal error: Call to undefined method MDB2_Error::query() in /home/htdocs/test/sql.lib.php on line 62 sql.lib.php is the name of the file which includes the code below. Test script: --------------- <?php // define the query types define('SQL_NONE', 1); define('SQL_ALL', 2); define('SQL_INIT', 3); // define the query formats define('SQL_ASSOC', 1); define('SQL_INDEX', 2); class SQL { var $db = null; var $result = null; var $error = null; var $record = null; /** * class constructor */ function SQL() { } /** * connect to the database * * @param string $dsn the data source name */ function connect($dsn) { $this->db = MDB2::connect($dsn); if(PEAR::isError($this->db)) { $this->error = $this->db->getMessage(); return false; } return true; } /** * disconnect from the database */ function disconnect() { $this->db->disconnect(); } /** * query the database * * @param string $query the SQL query * @param string $type the type of query * @param string $format the query format */ function query($query, $type = SQL_NONE, $format = SQL_INDEX) { $this->record = array(); $_data = array(); // determine fetch mode (index or associative) $_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC : null; $this->result = $this->db->query($query); if (PEAR::isError($this->result)) { $this->error = $this->result->getMessage(); echo $this->result->getMessage(); return false; } switch ($type) { case SQL_ALL: // get all the records while($_row = $this->result->fetchRow($_fetchmode)) { $_data[] = $_row; } $this->result->free(); $this->record = $_data; break; case SQL_INIT: // get the first record $this->record = $this->result->fetchRow($_fetchmode); break; case SQL_NONE: default: // records will be looped over with next() break; } return true; } /** * connect to the database * * @param string $format the query format */ function next($format = SQL_INDEX) { // fetch mode (index or associative) $_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC : null; if ($this->record = $this->result->fetchRow($_fetchmode)) { return true; } else { $this->result->free(); return false; } } } ?>

Comments

 [2010-05-07 17:58 UTC] quipo (Lorenzo Alberton)
-Status: Open +Status: Bogus -Assigned To: +Assigned To: quipo
--- In SQL::query() you are calling $this->db->query() without checking if you have a valid MDB2 object in $this->db first. Check the return value of the connect() call, it returns false on failure. Then check $sql->error for the error message.
 [2010-05-08 19:55 UTC] shadoan (Rene Wappenhans)
Hi Lorenzo, thanks for the advise, I could solve the problem in the database connection. A new problem I have: Using PEAR::DB a call like $memberinfos = $this->sql->query("SELECT member_nick, email, birthdate from members") returned a result like this: $memberinfos[member_nick] = "Max"; $memberinfos[email] = "test@domain.com"; $memberinfos[birthdate] = "1980-04-05"; The result included the table column names what was very helpful for coding. Now using PEAR::MDB2 the result is: $memberinfos[0] = "Max"; $memberinfos[1] = "test@domain.com"; $memberinfos[2] = "1980-04-05"; So I get the array without column names but upcounting array indexes. Do you have any idea how I can get the table column names?
 [2010-05-10 13:22 UTC] quipo (Lorenzo Alberton)