devdavad Posted November 30, 2008 Share Posted November 30, 2008 This may seem like a question for the postgresql board but I think that I'm doing something other than database work wrong. Anyway I ripped this off of an example on php.net (example 1: http://us3.php.net/manual/en/pdo.query.php) but I keep getting the error "Invalid argument supplied for foreach()" anyway, when I run it it does print connection established but then the error follows it <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; } catch(PDOException $e) { echo $e->getMessage(); } $aoeu = "SELECT events FROM calendar_" . date(F); foreach($db->query($aoeu) as $row) { echo $row["events"]; } $db = null; ?> If anyone could help me that would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/ Share on other sites More sharing options...
trq Posted November 30, 2008 Share Posted November 30, 2008 Your query is likely failing. Try.... $aoeu = "SELECT events FROM calendar_" . date('F'); Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702594 Share on other sites More sharing options...
devdavad Posted November 30, 2008 Author Share Posted November 30, 2008 Your query is likely failing. Try.... $aoeu = "SELECT events FROM calendar_" . date('F'); Unfortunately that produces the same error as well. By the way is it bad style to not quote the letters in the date function? I use the function a lot in the rest of that script and I do not quote the letter inside the date function. Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702602 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Umm...it's not a style thing; it's a syntax thing. Strings NEED to be quoted. You're lucky that PHP is extremely, extremely loose about syntax, otherwise you'd have a billion and two errors from that little mistake. You must quote EVERY string literal. Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702625 Share on other sites More sharing options...
corbin Posted November 30, 2008 Share Posted November 30, 2008 Move the query to inside of the try block and it should catch the error and tell you what it is. Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702635 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Why do you have different tables for each month? Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702643 Share on other sites More sharing options...
devdavad Posted December 1, 2008 Author Share Posted December 1, 2008 Umm...it's not a style thing; it's a syntax thing. Strings NEED to be quoted. ... thanks, DarkWater Move the query to inside of the try block and it should catch the error and tell you what it is. Unfortunately I'm getting the same error message with exceptions on. Why do you have different tables for each month? I'm trying to teach myself sql using postgresql. When designing that I knew that that would be cumbersome to work with but I also knew that I didn't know how else (at the time and right now) to hold one large calendar in one table and have the query only read the part of the table that is the current month. I didn't want to skip ahead and learn how to do that yet. I'm really glad I'm getting a lot of feedback on this post (instead of it reaching the bottom of the front page quickly ) Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702748 Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 try: <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; $aoeu = "SELECT events FROM calendar_" . date('F'); $result = $db->query($aoeu); $while($result->fetch() as $row){ echo $row["events"]; } }catch(PDOException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702754 Share on other sites More sharing options...
devdavad Posted December 1, 2008 Author Share Posted December 1, 2008 try: <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; $aoeu = "SELECT events FROM calendar_" . date('F'); $result = $db->query($aoeu); $while($result->fetch() as $row){ echo $row["events"]; } }catch(PDOException $e) { echo $e->getMessage(); } ?> Thanks for that . If I replace the $while part with foreach I get the error "Fatal error: Call to a member function fetch() on a non-object" which is a completely unrelated error to this thread. Currently I have made the following changes: <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; $sql = "SELECT events FROM calendar_" . date("F"); $conn->prepare($sql); $query = $conn->query($sql); foreach($query->fetch() as $row) { echo $row["event"]; } }catch(PDOException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702767 Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 how about: <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; $sql = "SELECT events FROM calendar_" . date("F"); if($result = $db->query($sql)){ foreach($db->query($sql) as $row) { echo $row["event"]; } }else{ print_r($dbh->errorInfo()); } }catch(PDOException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702772 Share on other sites More sharing options...
devdavad Posted December 1, 2008 Author Share Posted December 1, 2008 how about: <?php #include("connection.php"); try { $db = new PDO("pgsql:dbname=test;host=localhost", "david", "mypass"); echo "Connection established"; $sql = "SELECT events FROM calendar_" . date("F"); if($result = $db->query($sql)){ foreach($db->query($sql) as $row) { echo $row["event"]; } }else{ print_r($dbh->errorInfo()); } }catch(PDOException $e) { echo $e->getMessage(); } ?> All right that did it! Thank you very much sir. I found that I also couldn't have any tables with capital letters (since date("F") returns the month with the first letter capitalized), so that was another problem I was having. Quote Link to comment https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/#findComment-702782 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.