jgrauer Posted November 14, 2011 Share Posted November 14, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/ Share on other sites More sharing options...
codeprada Posted November 14, 2011 Share Posted November 14, 2011 Learn to refer to the manual. http://pear.php.net/manual/en/package.database.mdb2.intro-execute.php Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288086 Share on other sites More sharing options...
jgrauer Posted November 14, 2011 Author Share Posted November 14, 2011 You're joking right? Obviously I read the manual.. my example is built from the manual itself... Still looking for some help on this matter please. Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288094 Share on other sites More sharing options...
ManiacDan Posted November 14, 2011 Share Posted November 14, 2011 Per the manual, the array passed to execute() must have key names that match your bound param names. Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288097 Share on other sites More sharing options...
jgrauer Posted November 15, 2011 Author Share Posted November 15, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288201 Share on other sites More sharing options...
ManiacDan Posted November 15, 2011 Share Posted November 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288248 Share on other sites More sharing options...
jgrauer Posted November 15, 2011 Author Share Posted November 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288336 Share on other sites More sharing options...
ManiacDan Posted November 15, 2011 Share Posted November 15, 2011 Have you done any error-checking or error-handling? Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288356 Share on other sites More sharing options...
jgrauer Posted November 15, 2011 Author Share Posted November 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288378 Share on other sites More sharing options...
ManiacDan Posted November 15, 2011 Share Posted November 15, 2011 however the library was dying due to redeclaration of the same variables.You've been developing with error_reporting turned off. Never do that again. Quote Link to comment https://forums.phpfreaks.com/topic/251119-mdb2-prepare-statements-php-mysql/#findComment-1288387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.