Categories

View a sqlite table

This example shows how to view all the data in a sqlite table

<?php
//open db
if (!($db = sqlite_open(’sample.db’)))
{
    die(sqlite_error_string(sqlite_last_error($db)));
}

//select all results from db
$sql = ’select * from sample order by id asc’;
if (!($result = sqlite_query($db, $sql)))
{
    die(‘SQL ERROR: ‘ . sqlite_error_string(sqlite_last_error($db)));
}

//display all results
while ($row = sqlite_fetch_array($result))
{
    echo “{$row['id']}. {$row['cat']} . {$row['title']}<br />\n”;
}

//close
sqlite_close($db);
?>

Add to a sqlite database

<?php
//open db
if (!($db = sqlite_open(’sample.db’)))
{
    die(sqlite_error_string(sqlite_last_error($db)));
}
//data to be inserted
$cat = ’search’;
$title = ‘yahoo search’;
$sql = “insert into sample (cat, title) values (‘$cat’,’$title’)”;
//insert new data
if (!(sqlite_query($db, $sql)))
{
    die(‘SQL ERROR: ‘ . sqlite_error_string(sqlite_last_error($db)));
}
else
{
 echo “data entered”;
}

//close
sqlite_close($db);
?>

Create a sqlite database

This example creates a sample sqlite database and populates it with some data

<?php
//Open/Create sqlite db
if (!($db = sqlite_open(’sample.db’)))
{
    die(sqlite_error_string(sqlite_last_error($db)));
}

//create a table and populate with some data
$sql = ”
create table sample (
    id   integer  primary key,
 cat   text not null,
    title text     not null
);

insert into sample (cat, title) values (‘programming’,’getphp’);
insert into sample (cat, title) values (’sport’, ‘golf site’);
insert [...]

Get MySQL version

<?php
//displays MySQL version
printf (“MySQL client info: %s\n”, mysql_get_client_info());
?>

Connect to a MySQl database using PDO

This example uses the sample database table at http://www.getphp.net/sample-mysql-database/ , the database is called test and the table is also called test.

This will display all of the links and descriptions

<?php
//change these
$user = ‘root’;
$pass = ”;
try
{
 //localhost, test database
    $dbh = new PDO(‘mysql:host=localhost;dbname=test’, $user, $pass);
 //table is called test
    foreach($dbh->query(‘SELECT * from test’) as $row)
 {
  print “<a href={$row['url']}>{$row['name']}</a> : [...]

Display an Access database

Display an Access database, haven’t been able to check this but I’m sure this depends on the version of Access you have installed

<?php
$odbcdsn = “sampledb”;
$odbcuser = “”;
$odbcpass = “”;
$sqlquery = “SELECT * FROM tblsample”;
if(!($odbcdb = odbc_connect($odbcdsn , $odbcuser , $odbcpass)))
die(“cant open the dsn”);
if(!($odbcrs = odbc_do($odbcdb , $sqlquery)))
die(“cannot execute sql query”);
odbc_result_all($odbcrs);
?>