PHP Classes Tutorial 1 - Database Abstraction and Classes Tutorial - Page 5 of 7
More Methods
We are going to want some more advanced methods, I will be implementing the following:
getInsertID -- returns the id from the last INSERT query
getNum_Rows -- returns the number of rows generated by query
getDBList -- lists databases on the server
getTableList -- lists tables in the specified database
getFieldList -- lists fields in a specified table
createDB -- creates a database
dropDB -- drops a database
table_exists -- checks if a table exists in current database
function getInsertID()
{
return mysql_insert_id();
}
function num_rows( $result )
{
return mysql_num_rows( $result );
}
function getDBList()
{
return mysql_list_dbs();
}
function getTableList( $database )
{
return mysql_list_tables( $database );
}
function getFieldList( $database, $table )
{
return mysql_list_fields( $database, $table );
}
function createDB( $db_name )
{
$sql = 'CREATE DATABASE ' . $db_name;
mysql_query( $sql, $this->connect_id );
}
function dropDB( $db_name )
{
$sql = 'DROP DATABASE ' .$db_name;
mysql_query( $sql, $this->connect_id );
}
function table_exists( $table )
{
if( mysql_query( 'SELECT 1 FROM \'' . $table . '\' LIMIT 0', $this->connect_id ) )
{
return true;
}
else
{
return false;
}
}
Links open corresponding PHP manual page in new window
If you find you need any functions that are not included here then simply write you own, if you feel you have a function that would be useful to others email me, and I will be happy to include it. Check back as I will add functions as I find I need them, and they may be useful to you too.
