<?php
function db_connect() {
$host = 'localhost';
$user = 'root';
$pass = '273568';
$line = @mysql_connect($host, $user, $pass);
return $line;
}
function mysql_is_table($db,$tbl)
{
$line = db_connect();
$tables = array();
@mysql_select_db($db);
$q = @mysql_query("SHOW TABLES", $line);
while ($r = @mysql_fetch_array($q)) { $tables[] = $r[0]; }
@mysql_free_result($q);
@mysql_close($line);
if (in_array($tbl, $tables)) { return TRUE; }
else { return FALSE; }
}
function mysql_is_database($db)
{
$line = db_connect();
$databases = array();
$q = @mysql_query("SHOW DATABASES LIKE $db", $line);
while ($r = @mysql_fetch_array($q)) { $databases[] = $r[0]; }
@mysql_free_result($q);
@mysql_close($line);
if (in_array($db, $databases)) { return TRUE; }
else { return FALSE; }
}
// example usage
$newdbName = 'testDatabase';
$newdbTable = 'testTable';
if (mysql_is_database($newdbName)) {
echo 'Yes the news database is present';
if(mysql_is_table($newdbName,$newdbTable) {
echo 'Table is present';
} else {
mysql_query("CREATE TABLE " .$newdbTable);
if(mysql_error()) {
echo mysql_error();
//do some error trapping
}
}
} else {
mysql_query("CREATE DATABASE " .$newdbName);
if(mysql_error()) {
echo mysql_error();
//do some error trapping
}
}
Bookmarks