Bryce910 Posted April 27, 2012 Share Posted April 27, 2012 I am new and just learning PDO and already having some problems with very simple stuff <?php $hostname = "localhost"; $dbname = "PDO"; $username = "root"; $password = ""; try { $dbh = new PDO("mysql:$hostname;dname=$dbname",$username,$password); $sql = "SELECT * FROM users"; foreach($dbh->query($sql) as $row) { echo $row['id'] . $row['name'] . $row['email']; } echo "Number of effected rows:" . $count; } catch(PDOExecption $e) { echo $e->getMessage(); } ?> Can you tell me what in that code is making me get the error: Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\xampp\PDO\select_query.php and what I would need to alter to fix the problem. Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/ Share on other sites More sharing options...
xyph Posted April 27, 2012 Share Posted April 27, 2012 You can use the manual for problems like this. Foreach expects an array, you're supplying it with the return value from PDO->query(); According to the manual... http://php.net/manual/en/pdo.query.php ... PDO->query will either return FALSE, or a PDOStatement object, neither of which are arrays. PDOStatement->fetch or PDOStatement->fetchAll might help you out Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341227 Share on other sites More sharing options...
Bryce910 Posted April 27, 2012 Author Share Posted April 27, 2012 You can use the manual for problems like this. Foreach expects an array, you're supplying it with the return value from PDO->query(); According to the manual... http://php.net/manual/en/pdo.query.php ... PDO->query will either return FALSE, or a PDOStatement object, neither of which are arrays. PDOStatement->fetch or PDOStatement->fetchAll might help you out I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either? Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341231 Share on other sites More sharing options...
xyph Posted April 27, 2012 Share Posted April 27, 2012 I keep forgetting foreach will loop through objects, but my advice is still somewhat sound. If PDO->query isn't returning an object, what would it be returning, and why? Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341232 Share on other sites More sharing options...
Bryce910 Posted April 27, 2012 Author Share Posted April 27, 2012 To my understanding if it wasn't return an array of information it most likely isn't returning anything at all and the query is empty? Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341233 Share on other sites More sharing options...
kicken Posted April 27, 2012 Share Posted April 27, 2012 I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either? Did you create the database and table being used in the query? I'd guess your query is failing and ->query is returning FALSE rather than the statement object. Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341236 Share on other sites More sharing options...
Bryce910 Posted April 27, 2012 Author Share Posted April 27, 2012 I looked at that example and they do the same thing? I used this guide http://phpro.org/tutorials/Introduction-to-PHP-PDO.html to learn and I don't see any difference in that one either? Did you create the database and table being used in the query? I'd guess your query is failing and ->query is returning FALSE rather than the statement object. That is what I originally thought but then I tipple checked everything and the query shouldn't fail. Since I am just selecting all from the table. Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341237 Share on other sites More sharing options...
Bryce910 Posted April 27, 2012 Author Share Posted April 27, 2012 I can't get my select_query or my insert_query to work. Is it possible for it to be a problem with my version of xampp? The only PDO stuff that has worked is the connection process of PDO. Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341239 Share on other sites More sharing options...
xyph Posted April 27, 2012 Share Posted April 27, 2012 PDO won't throw an exception unless you tell it to http://www.php.net/manual/en/pdo.setattribute.php $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Now your query errors should throw exceptions, and be caught properly. Link to comment https://forums.phpfreaks.com/topic/261727-pdo-help/#findComment-1341243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.