Re: PEAR::MDB2

From: Date: Tue, 21 Nov 2006 20:11:15 +0000
Subject: Re: PEAR::MDB2
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-25899@lists.php.net to get a copy of this message
Damian Bursztyn wrote:
Hi, im trying to work with MDB2 and hace some problems. Maybe someone can help me. First, once i have a mdb2 connection on $mdb2 im trying to make a query using quote to prevent SQL Injections. $int = $mdb2->quote('11', 'integer'); $query = 'SELECT someDate, someNumber FROM myTable WHERE someInteger = ' . $int; var_dump($query); $result = $mdb2->exec($query, array('date', 'integer')); I suppouse that $query should be something like SELECT someDate, someNumber FROM myTable WHERE someInteger = 11 or maybe SELECT someDate, someNumber FROM myTable WHERE someInteger = '11' (i preffer this one, can i choose to put or not the quotes?) But instead of that the var_dump of query give me something like SELECT someDate, someNumber FROM myTable WHERE someInteger = Object id #2 How can i fix this? Because when i make $result = $mdb2->exec($query); an error takes place. Second, can i use prepare for select querys? i saw that the result object of prepare only have exec method. If i want to make a query with parameters (in the where condition) and i want to set types to that parameters. And i want to work my input data to prevent SQL injections and blind the variable (after filter it) the the parameter in the query wich is the best way? I was try to do the prepare with the types and then blinds the variables but i dont know if this makes the quote using the parameter type or not. Thanks for your help.
The exec() is operating on the prepared statement. If you're only SELECTing rows, add MDB2_PREPARE_RESULT to the prepare() call. If your statement will be modifying the table in any way, use MDB2_PREPARE_MANIP. Try: $your_integer = intval($whatever); $query = 'SELECT someDate, someNumber FROM myTable WHERE someInteger = ?'; $stmnt = $mdb2->prepare($query, Array('integer'), MDB2_PREPARE_RESULT); // note that you shouldn't really echo this out on a production site if (MDB2::isError($stmnt)) { die($stmnt->getUserInfo()); } $result = $stmnt->execute(Array($your_integer)); if (PEAR::isError($result)) { die($result->getUserInfo()); } /* call $mdb2->setFetchMode (MDB2_FETCHMODE_ASSOC) beforehand * or use $result->fetchRow(MDB2_FETCHMODE_ASSOC) */ while ($row = $result->fetchRow()) { ... } brian

« previous php.pear.general (#25899) next »