Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-05-2009
Xan Xan is offline
Member
 
Join Date: Jan 2009
Posts: 576
Check if database or table exists

I am new php learner and mysql and trying to creating pages for my project. I try to create it but fails to so. also i would like to enter docfile field that matches the record trying to entered into database.please help me to do so.
thank you
Reply With Quote
  #2  
Old 29-05-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Check if database or table exists

You can try mysqldump with the dump table with the add-drop option and inside.sql file

Quote:
The syntax for mysqldump : mysqldump --add-drop-table dbname tablename
also

$check = mysql_query ("SELECT * FROM `tableName` LIMIT 0,1"); /* >>limit<< is just to make it faster in case the db is huge */

if ($check){
// query was legal and could be executed by the server
}else{
// something wrong, so:
// create the table
}

Reply With Quote
  #3  
Old 29-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Check if database or table exists

try this code

Quote:
<?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
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Check if database or table exists"
Thread Thread Starter Forum Replies Last Post
Check if URL exists (Java) Kelvin Little Software Development 7 08-01-2010 04:04 PM
How to check if file exists in directory with Php Zool Software Development 3 03-11-2009 12:36 PM
How to check if variable exists in C# Hamlet Software Development 3 28-08-2009 07:30 PM
How to drop a table if exists in a SQL Script CE B_Hodge Software Development 2 09-06-2009 11:43 PM
Powershell: To check a directory exists or not? Chandrakant81 Software Development 3 18-02-2009 06:26 PM


All times are GMT +5.5. The time now is 02:44 AM.