Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/134915-solved-failing-with-foreach/
Share on other sites

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.

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.

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 :) )

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();
}

?>

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();
}
?>

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();
}
?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.