-
Notifications
You must be signed in to change notification settings - Fork 286
Quick Examples
Damien edited this page Aug 30, 2019
·
5 revisions
Note: In all these examples no other code is required other than including:
either for version 3.x
require 'ez_sql_loader.php';
or for version 4.x
// composer is required for version 4
require 'vendor/autoload.php';
Detailed installation instructions can be found on the Installation and Usage page.
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
// Get one row from the database and print it out..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;
// Get one variable from the database and print it out..
$var = $db->get_var("SELECT count(\*) FROM users");
echo $var;
// Insert into the database
$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','[email protected]')");
// Update the database
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");
Example 6 - debug()
// Display last query and all associated results
$db->debugOn();
$db->debug();
Example 7 - varDump()
// Display the structure and contents of any result(s) .. or any variable
$results = $db->get_results("SELECT name, email FROM users");
$db->varDump($results);
// Get 'one column' (based on column index) and print it out..
$names = $db->get_col("SELECT name,email FROM users", 0)
foreach ( $names as $name )
{
echo $name;
}
// Same as above ‘but quicker’
foreach ( $db->get_col("SELECT name,email FROM users", 0) as $name )
{
echo $name;
}
// Map out the full schema of any given database and print it out..
$db->select("my_database");
foreach ( $db->get_col("SHOW TABLES",0) as $table-name )
{
$db->debug();
$db->get_results("DESC $table-name");
}
$db->debug();