Quick Examples
Quick Examples
Note: In all these examples no other code is required other than including:
either for version 4.x
// composer is required for version 4 require 'vendor/autoload.php';
or for version 3.x
require 'ez_sql_loader.php';
Detailed installation instructions can be found on the Installation and Usage page.
1. query() for insert
<?php // Insert into the database. $db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");
2. query() for update
<?php // Update the database. $db->query("UPDATE users SET name = 'Justin' WHERE id = 2");
3. get_var()
<?php // Get one variable from the database and print it out. $var = $db->get_var("SELECT count(\*) FROM users"); echo $var;
4. get_row()
<?php // Get one row from the database and print it out. $user = $db->get_row("SELECT name,email FROM users WHERE id = 2"); // Access data using object syntax. echo $user->name; echo $user->email;
5. get_results()
<?php // 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; }
6. get_col()
<?php // 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; }
7. get_col() in foreach
<?php // Same as above ‘but quicker’ foreach ( $db->get_col("SELECT name, email FROM users", 0) as $name ) { echo $name; }
8. debug()
<?php // Enable debug $db->debugOn(); // Run a query $users = $db->get_results("SELECT name, email FROM users"); // Display last query and all associated results. $db->debug();
9. varDump()
<?php // Display the structure and contents of any result(s) .. or any variable $results = $db->get_results("SELECT name, email FROM users"); $db->varDump($results);
10. Print full schema
<?php // 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();