Search code examples
phppear

Pear DB method similar to bindParam()?


Is there any way using the PEAR DB library to do something similar to the PDO bindParam()? I have a couple prepared statements that are going to use the same variable multiple times so I'd like to be able to bind the value to the variable in the prepared statement. I just can't find a way to do it with Pear DB.

This is what I've started doing, but it is a bit of a pain.

$sSql = "SELECT EventID
            DAY(CONVERT_TZ(EventDate, ?, ?)) as EventDay,
            FROM RacingEvents WHERE
            MONTH(CONVERT_TZ(EventDate, ?, ?)) = ? AND
            YEAR(CONVERT_TZ(EventDate, ?, ?)) = ?";

    $aArgs = array();
    $aArgs[] = DEFAULT_TIMEZONE;
    $aArgs[] = $this->sTimezone;
    $aArgs[] = DEFAULT_TIMEZONE;
    $aArgs[] = $this->sTimezone;
    $aArgs[] = $this->iMonth;
    $aArgs[] = DEFAULT_TIMEZONE;
    $aArgs[] = $this->sTimezone;
    $aArgs[] = $this->iYear;

    if ($this->iSkill) {
        $sSql .= " AND NOT REPLACE(EventSkill, ?, '') = EventSkill";
        $aArgs[] = $this->iSkill;
    }

Solution

  • Pear's DB package is long dead, and you should either use MDB2 or PDO.

    MDB2 supports named parameters that you can pass to execute():

    <?php
    $types = array('integer', 'text', 'text');
    $sth = $mdb2->prepare('INSERT INTO numbers VALUES (:id, :name, :lang)', $types);
    
    $data = array('id' => 1, 'name' => 'one', 'lang' => 'en');
    $affectedRows = $sth->execute($data);