/*
This is just a quick example of how to use the basic database stuff.
*/
// setup a connection to the database "mydb"
$db = new DatabaseAbstract("mysql", "localhost", "username", "password", "mydb");
// turn debugging on so we see error messages
$db->debug = true;
// a simple query and results:
$db->sqlResult("SELECT * FROM table");
while ($row = $db->getRow()) {
echo $row["Name"]."
";
}
// a simple query with no results:
$db->sqlExec("INSERT INTO table (id, text) VALUES (1, 'test')");
// one result
// This is one of the handy features that just saves a couple
// lines of code
if ($row = $db->sqlRow("SELECT * FROM users WHERE UserID = 1")) {
echo "Name: ".$row["Name"]."
";
echo "Email: ".$row["Email"]."
";
} else {
echo "UserID 1 not found";
}
// two queries at once:
// (this could also be done with a join, but this is just to demonstrate)
$db->sqlResult("SELECT * FROM users");
while ($row = $db->getRow()) {
echo "User: ".$row["Name"];
// select a query into the "sub" result set
$db->sqlResult("SELECT * FROM items WHERE (UserID = '".$row["UserID"]."'", "sub");
while ($row = $db->getRow("sub")) {
echo "- ".$row["ItemName"]."
";
}
echo "
";
}
// insert a row, get the auto_increment id, and redirect to that page
// this is a handy feature
if ($userid = $db->sqlExec("INSERT INTO users (name) VALUES ($name)")) {
header("Location: viewuser.php?id=".$userid);
exit;
}
?>