And Finally we
display the result.
So what would this be like if we connected to a SQLite database?
$dsn = 'sqlite:abcd.db';
try
{
$pdo = new PDO($dsn);
$pdo->exec("CREATE TABLE users (id int, name VARCHAR)");
$pdo->exec("DELETE FROM users");
$pdo->exec("INSERT INTO users (name) VALUES('afif')");
$pdo->exec("INSERT INTO users (name) VALUES('tipu')");
$pdo->exec("INSERT INTO users (name) VALUES('robin')");
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$result = $pdo->query("select * from users");
foreach ($result as $row)
echo $row['name'];
?>
See there is no change in the code except the DSN.
Database in an OOP Way
[ 178 ]
You can also create a SQLite database in memory and perform the operation there.
Let's see the following code:
$dsn = 'sqlite::memory:';
try {
$pdo = new PDO($dsn);
$pdo->exec("CREATE TABLE users (id int, name VARCHAR)");
$pdo->exec("DELETE FROM users");
$pdo->exec("INSERT INTO users (name) VALUES('afif')");
$pdo->exec("INSERT INTO users (name) VALUES('tipu')");
$pdo->exec("INSERT INTO users (name) VALUES('robin')");
}
catch (PDOException $e)
{
echo 'Connection failed: ' .
Pages:
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192