Skip to content

Instantly share code, notes, and snippets.

@solicomo
Created January 3, 2014 10:36
Show Gist options
  • Save solicomo/8235969 to your computer and use it in GitHub Desktop.
Save solicomo/8235969 to your computer and use it in GitHub Desktop.
test mysql/sqlite/pdo
<?php
mysql_connect("host","username","password") or die("Unable to Connect");
mysql_select_db("dbname") or die("Could not open the db");
$sql="select * from tablename";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row[0].' => '.$row[1]."\n";
}
<?php
$dbh = new PDO('mysql:host=localhost;dbname=dbname;', 'username', 'password');
$sql="select * from tablename";
foreach ($dbh->query($sql) as $row)
{
echo $row[0].' => '.$row[1]."\n";
}
<?php
$dbh = new PDO('sqlite:test.sqlite');
$sql='CREATE TABLE tab(key PRIMARY KEY, val)';
$dbh->exec($sql);
$sql='INSERT INTO tab VALUES("k1", "v1")';
$dbh->exec($sql);
$sql='INSERT INTO tab VALUES("k2", "v2")';
$dbh->exec($sql);
$sql="select * from tab";
foreach ($dbh->query($sql) as $row)
{
echo $row[0].' => '.$row[1]."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment