Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: new db class


Message #1 by "Vincent, Justin" <J.Vincent@e...> on Mon, 14 Oct 2002 14:17:17 +0100
If you regularly work with databases in your scripts 
then you might be interested in a widget that I have 
written called ez_sql.php (avail from: http://php.justinvincent.com)

All you have to do is include one file and then you 
can do lots of db related things 'very easily' and 
efficiently (since it is v small and last results are always cached).

If you try it please let me know what you think of it.

Cheers,
Justin.

Quick Examples..

Note: In all these examples no other code is required other 
      than including ez_sql.php

----------------------------------------------------
Example 1
----------------------------------------------------

// Select multiple records from the database and print them out..
if ( $users = $db->get_results("SELECT name, email FROM users"))
{
	foreach ( $users as $user )
	{

		// Access data using object syntax
		echo $user->name;
      	echo $user->email;
	}
}
else
{
	echo "No Users!";
}
 

----------------------------------------------------
Example 2
----------------------------------------------------

// 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;


----------------------------------------------------
Example 3
----------------------------------------------------

// Get one variable from the database..
if ( $password == $db->get_var("SELECT password FROM users WHERE uid = 22"))
{
	echo "Correct Password!";
}
 

----------------------------------------------------
Example 4
----------------------------------------------------

// Insert/Update the database
$db->query("INSERT INTO users (id, name, email) VALUES
(NULL,'justin','jv@f...')");
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");
 


----------------------------------------------------
Example 5
----------------------------------------------------

// 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();

----------------------------------------------------



  Return to Index