Results 1 to 1 of 1

Thread: help with PDO error

  1. #1
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default help with PDO error

    Hi, I'm brand new to PDO, so I would appreciate all the help I can get. I'm using PDO to query a SQLite database I set up earlier for testing. I am getting an error that says,
    Warning: Invalid argument supplied for foreach() in (path removed) on line 38
    line 38 is highlighted below. I have been rewriting it for about an hour now.
    PHP Code:
    try {
        
    $dbh = new PDO("sqlite:".$_SERVER['DOCUMENT_ROOT']."/reel/demo/a.sqlite");
        echo 
    "Connected again";
        
    $sql "SELECT * FROM moulding";
        foreach (
    $dbh->query($sql) as $row){
            
    //line 38 below
            
    echo $row['part'] .' '$row['size'] .' '$row['type'] .' '$row['species'];
          }
          
    $dbh null;
        } catch (
    PDOException $e) {
          print 
    "Error!: " $e->getMessage() . "<br/>";
          die();
        } 
    thanks in advance, everyone

    Edit:
    I got part of it figured out. The argument was invalid because the try{} block above it was not successfully creating the table and inserting the records. I've got it working with a different DB I built using SQLite manager. I would however, appreciate some advice on how to fix the first block (below) to update my DB via PHP:

    PHP Code:
    try {
      
    $dbh = new PDO("sqlite:".$_SERVER['DOCUMENT_ROOT']."/reel/demo/a.sqlite");
      echo 
    "Connected\n";
      
    $dbh->exec("CREATE TABLE moulding(part varchar(10) primary not null, size varchar(25) not null, type varchar(25) not null, species varchar(25) not null)");
      
    $count $dbh->exec("INSERT INTO moulding (part, size, type, species) VALUES (1060, '19/6 x 4', 'base', 'cherry')");
      echo 
    $count;
      
    $dbh null;
    } catch (
    Exception $e) {
      echo 
    "Failed: " $e->getMessage();

    Edit: sorry, guys. I just stepped into PDO last night, and I really expected it to be harder. I'm sure I'll have a real problem soon enough, though, so 'till then...
    peace out, and thanks
    Last edited by traq; 05-22-2009 at 04:34 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •