0

I am trying to persist and retrieve images via PDO using USERCAKE2.0.

I am getting a invalid query error in the following method.

Exact error is:

PHP Fatal error: Call to a member function prepare() on a non-object in line 51

which translates to the method get_cert's prepare statement.

public function get_cert($certificate_number,$issue_date){
    global $mysqli,$db_table_prefix;
    $result = array();
    $stmt = $mysqli->prepare("select * from ".$db_table_prefix."certs where cert_num=? and issue_date=?");
    $stmt->bind_param('ss',$certificate_number,$issue_date);
    $stmt->execute();
    $stmt->store_result();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $result=$stmt->fetch();
    return $result;
}

The insert query for the same table is:

public function persist_cert(){
        global $mysqli,$db_table_prefix;
        $stmt = $mysqli->prepare('insert into '.$db_table_prefix.'certs (cert_num,certificate,access_url,issue_date) values (?,?,?,?)');
        $null = NULL;
        $stmt->bind_param('sbss',$this->cert_num,$null,$this->access_url,$this->issue_date);
        $stmt->send_long_data(2,$this->certificate);
        $this->id = $stmt->execute();
        return empty($this->id);
    }

The table structure is :

$certificate_sql = "CREATE TABLE IF NOT EXISTS `".$db_table_prefix."certs` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `cert_num` varchar(250) NOT NULL,
    `certificate` blob NOT NULL,
    `access_url` varchar(200) NOT NULL,
    `issue_date` date NOT NULL,
    PRIMARY KEY (`id`),
        UNIQUE KEY (`cert_num`,`issue_date`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    ";

NB - $db_table_prefix and $mysqli are predefined constants in usercake 2.0.

Please let me know what is wrong in the code above. Why am I getting the error?

deceze
  • 510,633
  • 85
  • 743
  • 889
Srihari
  • 766
  • 1
  • 6
  • 22
  • @Fred-ii- thanks 4 reply. Issue persists after change to `ss` – Srihari Aug 14 '14 at 19:29
  • 1
    You're welcome. The error is this: You're mixing MySQL APIs with `bind_param` and PDO `$stmt->setFetchMode(PDO::FETCH_ASSOC);`, they do **not** mix. Use a `mysqli_` equivalent. Plus, make sure your DB connection is not PDO neither. That should solve it. – Funk Forty Niner Aug 14 '14 at 19:30
  • 1) Nothing about this seems to have anything to do with PDO. 2) The error is very self explanatory, see linked duplicate. 3) We cannot tell you why `$mysqli` is not an object, because we're not seeing that part of your code. – deceze Aug 14 '14 at 19:32
  • @deceze - $mysqli is a declared global variable. It is used throughout usercake. The only place when anybody recieves this error is when the associated query is wrong. – Srihari Aug 14 '14 at 19:34
  • 1
    @Fred-ii- removing `PDO::FETCH_ASSOC` did the trick. thanks – Srihari Aug 14 '14 at 19:35
  • You're welcome, was glad to have helped. *Cheers* – Funk Forty Niner Aug 14 '14 at 19:36

0 Answers0