3raser Posted April 7, 2012 Share Posted April 7, 2012 I've been playing around with PDO lately, and I've been trying to get use to the basic functions as I've mentioned in some of my other posts/questions. Recently I switched to the ? bind/token (can't remember it's called), so I reformatted my processQuery. I've also tried to add in a fetching option in the method, but I can't seem to get anything to work. I've looked up several internet tutorials but it seems like I'm doing everything the same. Yet, the query doesn't seem to run. :/ $database->processQuery2("INSERT INTO test (test, testa) VALUES (?, ?)", array('noob', 'newb2'), false); Now for my method: public function processQuery2($query, array $binds, $fetch) { $query_handle = $this->dbc->prepare($query); if(count($binds) > 0) { $i = 1; $value = array(); foreach($binds as $bind) { $query_handle->bindParam($i, $value[$i]); $value[$i] = $bind; $i++; } } $query_handle->execute(); //if($fetch == true) //{ //return $query_handle->fetchAll(); //} } Any reasons to why it's not working? No errors are outputted, either. Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/ Share on other sites More sharing options...
ignace Posted April 7, 2012 Share Posted April 7, 2012 $query_handle->bindParam($i, $value[$i]); should be: $query_handle->bindParam($i, $binds[$i]); The reason you are not getting any errors is because you don't have error reporting set to On (at the very least you would have gotten something like `Undefined index` since $value is empty) or display errors have been turned Off. error_reporting(E_ALL); ini_set('display_errors', 1); For best effect these should be turned on in your php.ini Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335152 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2012 Share Posted April 7, 2012 You can also just use - $query_handle->execute($binds); Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335161 Share on other sites More sharing options...
3raser Posted April 7, 2012 Author Share Posted April 7, 2012 Thanks for the replies guys. I did alter my code like so: public function processQuery2($query, array $binds, $fetch) { $query_handle = $this->dbc->prepare($query); $query_handle->execute($binds); if($fetch == true) { return $query_handle->fetchAll(); } } Yet, no success. :/ Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335242 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2012 Share Posted April 7, 2012 As ignace stated, you likely don't have error_reporting/display_errors set so that you would be getting any php detected errors. $this->dbc might not be a valid PDO instance. Your prepare() might be failing due to an error in the query statement. What have you done to debug what your code is doing? Using var_dump on $this->dbc and on $query_handle might tell you what is working and what is not. Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335247 Share on other sites More sharing options...
3raser Posted April 7, 2012 Author Share Posted April 7, 2012 I turned error reporting on, but no errors are outputted/returned. But I do do var_dump for both my PDO dbc object and the query_handle: object(PDO)#2 (0) { } object(PDOStatement)#3 (1) { ["queryString"]=> string(53) "INSERT INTO users VALUES (id, ?, ?, ?, ?, ?, ?, ?, ?)" } Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335248 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2012 Share Posted April 7, 2012 Some error checking and error reporting logic for your code - <?php if(!$query_handle->execute()){ $error = $query_handle->errorInfo(); echo $error[2]; } Quote Link to comment https://forums.phpfreaks.com/topic/260489-queries-not-working-with-pdo/#findComment-1335249 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.