traq
05-21-2009, 08:13 PM
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.
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
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:
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();
}
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
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.
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
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:
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();
}
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