1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Validate if table and records exists

Discussion in 'PHP' started by piropeator, May 15, 2017.

  1. #1
    I want to validate if the table exists, if that is true, check if there are records or not. I have this code, but the system show this message in query line whe the table is not exists. (Using PHP/PDO).
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146
    PDOException: SQLSTATE[42S02]: Base table or view not found: 1146

    <?php
    require ('includes/configure.php');
    $db = new connection();
    $sql = "SELECT cod, nom FROM table";
    $stm = $db->query($sql);
    if (!$stm){
        echo "There are problems";
    } else {
        if ($stm->rowCount() > 0) {
            echo "There are records";
        } else {
            echo "There are not records";
        }
    }
    ?>
    PHP:
    What is the mistake?
     
    Solved! View solution.
    piropeator, May 15, 2017 IP
  2. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #2
    Code snippet that you have posted doesn't say much. Either you are looking for the wrong table or .. you are looking for the wrong table (in most of the cases).
     
    Blank ™, May 15, 2017 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    Hi. Of course, I try to validate is wrong table, is that true the code should show "There are problems", but doesnt do that.
     
    piropeator, May 15, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    If that is what is being returned if you try to do stuff to a query with a non-existing table, your DB-code is crap. It should just return an error, not a fatal error - my class would return the following:
    
    <p class="messagebox error visible">Attempt to execute on boolean - check logs for errors SELECT * FROM table</p>
    
    Code (markup):
    Granted, the code being output is controlled by the DB-class, and the error-output is custom.
     
    PoPSiCLe, May 15, 2017 IP
  5. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #5
    Mi db conection is this:
    <?php
    class connection extends PDO {
        public function __construct () {
            try {
                    parent:: __construct('mysql:host=localhost;dbname=mydb', user, password);
                    parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                } catch (Exception $ex) {
                        die ('Data base doesnt exists');
            }
        }
    
        function __destruct(){
        }
    }
    ?>
    PHP:
     
    piropeator, May 15, 2017 IP
  6. #6
    Remove the
    
    parent:: setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    Code (markup):
    That will remove the fatal error - then handle the error properly.
     
    PoPSiCLe, May 15, 2017 IP
  7. Harshal shah

    Harshal shah Active Member

    Messages:
    126
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    65
    #7
    <?php
    require ('includes/configure.php');
    $db = new connection();
    $sql = "SELECT cod, nom FROM table";
    $stm = $db->query($sql);
    if (!$stm){
    echo "There are problems";
    } else {
    if ($stm->rowCount() > 0) {
    echo "There are records";
    } else {
    echo "There are not records";
    }
    }
    ?>


    In this if you table name is table then you need to do like this

    SELECT cod, nom FROM `table`

    If you will use direct table it will give error because its a default syntax.
     
    Harshal shah, May 17, 2017 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    The mistake is that you are running a query on a non-existent table.
    Here's the code I use. Its for mysqli

    $result = $mysqli->Query(" SHOW TABLES LIKE 'my_table_name' ");
    if( $result->num_rows >0 ){
    // table exists run other queries

    }else{
    echo ' no table ';
    }
     
    JEET, May 17, 2017 IP
    ThePHPMaster likes this.