Devistation Posted August 29, 2012 Share Posted August 29, 2012 Hey everybody, I would first like to say that I am looking forward to becoming part of the community. I've got a bit of a dilema which I have hoping you guys and girls can help me fix, also, I apologise if I have placed this topic in the wrong place. I am quite new to both PDO and OOP so I apologise in advance for any obvious errors. classes.php class dbConnect { private $dbHost; private $dbUsername; private $dbPassword; private $dbName; function dbLink() { //Database host. $dbHost = "localhost"; //Database username. $dbUsername = "username"; //Database password. $dbPassword = "password"; //Database name. $dbName = "name"; //Open up a connection to the database $dbConnection = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword, array(PDO::ATTR_PERSISTENT => TRUE)); return $dbConnection; } function prepareQuery($statement, $array) { $dbConnectTrue = $this->dbLink(); $dbqueryPrepare = $dbConnectTrue->prepare($statement); $dbqueryPrepare->execute(array($array)); //<------ LINE 29 $queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC); return $queryFetch; } } phppage.php //Open up the connection to the database class. $dbConnectionLink = new dbConnect(); $errorReportFetch = $dbConnectionLink->prepareQuery("SELECT * FROM information WHERE id = ? AND name = ?", array("1", "error")); $errorReportStatus = $errorReportFetch['status']; if ($errorReportStatus == 1) { //If error reporting is equal to 1 then this will print any and all PHP errors. error_reporting(-1); } My aim is to try and make it so that I send the query to the class function as well as the WHERE clauses in an array, but I keep getting the following errors... Notice: Array to string conversion in classes.php on line 29 Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in classes.php on line 29 What I am realistically trying to do with this class is connect to the database and run a query, whether that be a SELECT, INSERT, UPDATE or DELETE query. I hope this makes sense to someone, I am not the best at describing things, and I apologise if there are any trivial mistakes, I have changed a few things around so that it isn't exactly the same as my code (variables ect). Thank you in advance. Devistation Quote Link to comment https://forums.phpfreaks.com/topic/267766-oop-and-pdo-question/ Share on other sites More sharing options...
requinix Posted August 29, 2012 Share Posted August 29, 2012 execute does not take any arguments. You have to call bind_param first. Problem is bind_param() needs values by reference so you can't just call_user_func_array() it. Needs a little more: function prepareQuery($statement, array $array = array()) { $dbConnectTrue = $this->dbLink(); $dbqueryPrepare = $dbConnectTrue->prepare($statement); if ($array) { $copy = array(); foreach ($array as $key => $value) { $copy[] =& $array[$key]; } call_user_func_array(array($dbqueryPrepare, "bind_param"), $copy); } $dbqueryPrepare->execute(); $queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC); return $queryFetch; } Quote Link to comment https://forums.phpfreaks.com/topic/267766-oop-and-pdo-question/#findComment-1373640 Share on other sites More sharing options...
Devistation Posted August 29, 2012 Author Share Posted August 29, 2012 Hey requinix, Thanks for getting back to me... I added in the code that you suggested, only now I get the following errors... Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'PDOStatement' does not have a method 'bind_param' in classes.php on line 37 Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in classes.php on line 41 Line 37 being the call_user_func_array line and Line 41 being the execute() line. Devistation Quote Link to comment https://forums.phpfreaks.com/topic/267766-oop-and-pdo-question/#findComment-1373662 Share on other sites More sharing options...
Devistation Posted September 1, 2012 Author Share Posted September 1, 2012 I decided to go for a slightly different approach which seems to work. function prepareQuery($statement, $array) { $dbConnectTrue = $this->dbLink(); $dbqueryPrepare = $dbConnectTrue->prepare($statement); $arrayStart = 1; foreach ($array as $arrayStatement) { $dbqueryPrepare->bindParam($arrayStart, $arrayStatement); $arrayStart++; } $dbqueryPrepare->execute(); $queryFetch = $dbqueryPrepare->fetch(PDO::FETCH_ASSOC); return $queryFetch; } Seems to be working for me. Thanks for your help though. Devistation Quote Link to comment https://forums.phpfreaks.com/topic/267766-oop-and-pdo-question/#findComment-1374520 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.