Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:37:36+00:00 2026-06-11T21:37:36+00:00

I’m using PHP 5.1.6 and MDB2 and trying to wrap my prepare/execute/fetchAll into a

  • 0

I’m using PHP 5.1.6 and MDB2 and trying to wrap my prepare/execute/fetchAll into a class so I can execute a select query with one line. The following code shows the Class that I’ve created and also doing the same query directly:

<?php
include_once "MDB2.php";

class db {

    private static $dsn = array(
        'phptype'  => "mysqli",
        'username' => "username",
        'password' => "pass",
        'hostspec' => "localhost",
        'database' => "dbname"
    );

    private static $instance = NULL;
    private static $statement = NULL;
    private static $resultset = NULL;

    private function __construct() {}
    private function __clone() {}

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance =& MDB2::factory(self::$dsn);
        }
        return self::$instance;
    }

    public static function execQuery($sql, $types, $values) {
        if (self::$instance === NULL) {
            self::getInstance();
        }
        self::$statement = self::$instance->prepare(
                $sql, $types, MDB2_PREPARE_RESULT);
        self::$resultset = self::$statement->execute(array($values));  
        if (PEAR::isError(self::$resultset)) {
            // (this is where it fails)
            echo('Execute Failed: ' . self::$resultset->getMessage());
            return false;
        }
        return self::$resultset->fetchAll(MDB2_FETCHMODE_ASSOC);
    }
}

echo "<pre>";

$dsn = array(
    'phptype'  => "mysqli",
    'username' => "username",
    'password' => "pass",
    'hostspec' => "localhost",
    'database' => "dbname"
);

$sql = "select * from testtable where id = ? order by id LIMIT ?"
$t = array('text','integer');
$v = array('ABC',3);

// GOING DIRECT 
$db   =& MDB2::factory($dsn);
$stmt =  $db->prepare($sql, $t, MDB2_PREPARE_RESULT);
$res  =  $stmt->execute($v);  
$out  =  $res->fetchAll(MDB2_FETCHMODE_ASSOC);
print_r($out);

// GOING THROUGH CLASS
print_r( db::execQuery($sql, $t, $v) );

?>

The output of going direct works as expected, but the second attempt, going through the class fails with the PEAR error message “MDB2 Error: not found”. I can’t see what the difference is between these two approaches.

The Class works properly if I only pass a SQL statement with one ? replacement and don’t use ‘array()’ to hold the types and values. If I change these, it works:

$sql = "select * from testtable where id = ? order by id"
$t = 'text';
$v = 'ABC';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:37:38+00:00Added an answer on June 11, 2026 at 9:37 pm

    I found the problem with my code. The line in the execQuery method that reads:

    self::$resultset = self::$statement->execute(array($values));
    

    should be:

    self::$resultset = self::$statement->execute(      $values );
    

    OOP is new to me so I was convinced the problem was with the class and method.

    (I’m not sure if I should be answering my own question, or just put it in the comments)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.