Results 1 to 3 of 3

Thread: Check if database or table exists

  1. #1
    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

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Check if database or table exists

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

    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
    }


  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Check if database or table exists

    try this code

    <?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
    }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,613,841.18902 seconds with 16 queries