- Edited
Hi guys!
Im fairly new to PHP and I am trying to submit some data to an sqlite database, however it seems to not be working as intended. When I run the file, I get "Warning: Invalid argument supplied for foreach() in line 21". Not sure why, as the parameters are appropriate. I suspect its because the specified table isnt being made.
NB: I am using DB Browser for SQLITE.
Any assistance would be appreciated! Thanks!
<?php
try {
$db = new PDO('sqlite:newTest.db');
$db->exec("CREATE TABLE userinfo(id INTEGER PRIMARY KEY, name TEXT, surname TEXT, age TEXT");
$db->exec("INSERT INTO userinfo(id, name, surname, age) VALUES (1, 'nick', 'francis', '21')'; ");
$db->exec("INSERT INTO userinfo(id, name, surname, age) VALUES (2, 'tris', 'test', '23')'; ");
$db->exec("INSERT INTO userinfo(id, name, surname, age) VALUES (3, 'john', 'doe', '24')'; ");
print "<table border = 1>";
print "<tr><td>ID</td><td> Name </td> <td> Surname </td> <td> Age </td> <tr>";
$result = $db->query('SELECT * from userinfo');
foreach($result as $row) {
print "<tr><td>".$row['id']."</td>";
print "<td>".$row['name']."</td>";
print "<td>".$row['surname']."</td>";
print "<td>".$row['age']."</td></tr>";
};
print "</table>";
} catch(PDOException $e) {
echo $e-> getMessage();
};
?>