Jump to content

MDB2 Prepare Statements PHP + MySQL


jgrauer

Recommended Posts

hey... found this website when googling for a php help forum... hopefully this is the right place.

 

I am about to start a major project for a new high profile company contract my company is presently bidding on. One of the requirements is a web interface to communicate between the 2 companies.

 

I recently read (and have been reading more into it) that MDB2 is (one of) the best methods to communicate with a database (using prepared statements).

 

It seems to be extremely popular, and not to difficult to use... however, for some reason, it just wont work for me.

 

<?php
require_once '/include/MDB2.php';

function connect(){
    $SQL = array(
        'driver' => 'mysql',
        'user' => '***',
        'pass' => '***',
        'host' => '127.0.0.1',
        'dbname' => '***',
    );

    $SQL['sql'] = $SQL['driver']."://".$SQL['user'].":".$SQL['pass']."@".$SQL['host']."/".$SQL['dbname'];

    $mdb2 = MDB2::connect($SQL['sql']);
    $mdb2->setOption('emulate_prepared',true);

    if(PEAR::isError($mdb2))die("Error while connecting : " . $mdb2->getMessage());

    return $mdb2;
}

$mdb2 = connect();
$statement = $mdb2->prepare("INSERT INTO `test` (id,name) VALUES (?, ?)", array('integer','text'), MDB2_PREPARE_MANIP);
$statement->execute(array(1,'someuser'));
$statement->free();
?>

 

For some reason, this is not working? However if I just execute a normal query, it works no problem:

 

<?php
$mdb2 = connect();
$statement = $mdb2->query("INSERT INTO `test` (id,name) VALUES (1,'someuser')");
?>

 

So, I am connecting properly, everything is good... but this prepared statement just hates me :( Any help please?

Link to comment
Share on other sites

this is only in the case that I use namespaces such as :name or something. However, if I simply use ? , it should just read from the array , in the same order I provide it?

 

Does no one on this forum have experience with this? This isn't that new?

Link to comment
Share on other sites

How does your code differ from the manual?  From what I saw, you were using ? instead of named params.  Are you sure those work?  I didn't read the whole manual because I'm not trying to get this to work, but what I saw all had named params.  Find the section that does it your way, see what's different.

 

Link to comment
Share on other sites

On the page : http://pear.php.net/manual/en/package.database.mdb2.intro-execute.php

 

Passing an array to execute()
<?php
// Once you have a valid MDB2 object named $mdb2...
$types = array('integer', 'text', 'text');
$sth = $mdb2->prepare('INSERT INTO numbers VALUES (?, ?, ?)', $types, MDB2_PREPARE_MANIP);

$data = array(1, 'one', 'en');
$affectedRows = $sth->execute($data);
?>

 

It seems, that there are both options.. I have tried both options, and both don't work.

 

As you can see, my code is nearly identical to this one (except, I am running only 2 fields instead of 3).

 

This is blowing my mind away.. and I've posted in my more then one place for help :(

Link to comment
Share on other sites

Finally after many days of banging my head on the desk, I have figured out the problem.

 

I had copied over the PEAR and MDB2 libraries over into my include folder, and was referencing them there. However, it seems somewhere in the PEAR library, it already referenced MDB2 and was creating some sort of loop when trying to do a prepared statement.

 

All the code I wrote was completely accurate, however the library was dying due to redeclaration of the same variables.

 

I simply stopped referencing the PEAR and MDB2 libraries in my include folder, and simply included the ones form the php directory itself.

 

Everything is working fine now.

 

Thanks for the help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.