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?
    SEMrush
     
    Solved! View solution.
    piropeator, May 15, 2017 IP
    SEMrush
  2. Blank ™

    Blank ™ Well-Known Member

    Messages:
    220
    Likes Received:
    17
    Best Answers:
    5
    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 Active Member

    Messages:
    192
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #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,627
    Likes Received:
    716
    Best Answers:
    151
    Trophy Points:
    420
    #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 Active Member

    Messages:
    192
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #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 Member

    Messages:
    92
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #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 Well-Known Member

    Messages:
    2,160
    Likes Received:
    97
    Best Answers:
    1
    Trophy Points:
    170
    #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.