Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    detect if db table exists by php

    I know how to create a table:
    http://www.w3schools.com/php/php_mysql_create.asp
    But how can I detect if the table exists. And if it doesn't, then create one using php?
    Compare bible texts (and other tools):
    TheWheelofGod

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you try to select a row from the table you'll receive error "1146" back if the table doesn't exist. You can use the mysql_errno() function to check for this error.

    Full list of error numbers:

    http://dev.mysql.com/doc/refman/5.1/...es-server.html

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,065
    Thanks
    2
    Thanked 317 Times in 309 Posts
    Or depending on the context of when you want to do this, just use the IF NOT EXISTS form of the CREATE TABLE syntax -

    CREATE TABLE IF NOT EXISTS tbl_name
    (create_definition,...)
    [table_option] ...
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Oh yes, that is a better solution. I was thinking there was an option like that for CREATE TABLE, but I got distracted and forgot to look it up.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    Or depending on the context of when you want to do this, just use the IF NOT EXISTS form of the CREATE TABLE syntax -
    this is the url:
    book1=1&chapter1=46&verse1=all&book2=2&chapter2=1&verse2=all&txtarea0=these are the names of the children of Israel, which came into Egypt
    For some reason after book1 everything else shows blank.
    PHP Code:
    <?php 
    $db 
    "kjv";
    $table "findings";
    include(
    "dbconnection.php");
        
    // Create table
        
    mysql_select_db($db$con);
        
    $sql "CREATE TABLE IF NOT EXISTS findings
        (
        ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        book1 int,
        chapter1 int,
        verse1 VARCHAR(4),
        book2 int,
        chapter2 int,
        verse2 VARCHAR(4),    
        txtarea0 VARCHAR(60),
        txtarea1 VARCHAR(60),
        txtarea2 VARCHAR(60),
        txtarea3 VARCHAR(60),
        txtarea4 VARCHAR(60),
        txtarea5 VARCHAR(60),
        comments VARCHAR(60)
        )"
    ;
        
        
    // Execute query
        
    mysql_query($sql$con);
        
    //put your table creation code here
    //}
    /************************************************************************************************/
        
    $con mysql_connect("","root","");
        if (!
    $con)
          {
          die(
    "Could not connect: ".mysql_error());
          }
        
    mysql_select_db($db$con);
        
    /*
        book1=1&chapter1=46&verse1=all&book2=2&chapter2=1&verse2=all&txtarea0=these are the names of the children of Israel, which came into Egypt
        */
        
    mysql_query("INSERT INTO findings (
        book1,
        chapter1,
        verse1,
        book2,
        chapter2,
        verse2,    
        txtarea0,
        txtarea1,
        txtarea2,
        txtarea3,
        txtarea4,
        txtarea5,
        comments
        ) VALUES (
        '"
    .$_GET['book1']."',
        '"
    .$_GET['chapter1']."',
        '"
    .$_GET['verse1']."',
        '"
    .$_GET['book2']."',
        '"
    .$_GET['chapter2']."',
        '"
    .$_GET['verse2']."',    
        '"
    .$_GET['txtarea0']."',
        '"
    .$_GET['txtarea1']."',
        '"
    .$_GET['txtarea2']."',
        '"
    .$_GET['txtarea3']."',
        '"
    .$_GET['txtarea4']."',
        '"
    .$_GET['txtarea5']."',
        '"
    .$_GET['comments']."'
        )"
    );
    echo 
    $_GET['book1'].", ".$_GET['chapter1'].", ".$_GET['verse1'].", ".$_GET['book2'].", ".$_GET['chapter2'].", ".$_GET['verse2'].",    ".$_GET['txtarea0'].", ".$_GET['txtarea1'].", ".$_GET['txtarea2'].", ".$_GET['txtarea3'].", ".$_GET['txtarea4'].", ".$_GET['txtarea5'].", ".$_GET['comments']." are inserted into the database!";
    mysql_close($con);
    ?>
    Last edited by gilgalbiblewhee; 03-04-2009 at 02:34 AM.
    Compare bible texts (and other tools):
    TheWheelofGod


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •