samedi 9 janvier 2016

PHP PDO SQLite General error: 1 no such table on Query only. Can Update fine

So, working on this little personal project, and run into the strangest error.

I have a table, Players. I can write to it just fine. I use a few lines of code to check the count in the table, then use that number to iterate the primary key.

When I wrote a function that uses these same lines of code just to check if there is data at all before producing a visible table...

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 no such table: Players'

Here's the snippet of corresponding code for the update, which works fine.

function Add_Player($PlayerName){
    #First, the function finds out how many players currently exist, so it can assign a PlayerID
    $statement="SELECT count() FROM Players;";

    $this->data_set = $this->db->query($statement);
    if($this->data_set){ #THIS SUCCEEDS
        while ($row = $this->data_set->fetch()){
                $Index_Num=$row['count()']+=1;
        }
    }else{
        $Index_Num=1;
    }
    #Then the function adds the New Player
    $statement='INSERT INTO Players VALUES ('.$Index_Num.',"'.$PlayerName.'");';    
    $SQL_statement = $this->db->prepare($statement);
    $SQL_statement->execute();

}

Here's the code that breaks on the view,

function Table_of_Players(){
    #First, it polls the database to see if the Table has data
    $statement="SELECT count() FROM Players;";
    $this->data_set = $this->db->query($statement);
    if($this->data_set){  #THIS FAILS
        while ($row = $this->data_set->fetch()){
                $Index_Num=$row['count()'];
        }

        if($Index_Num>=1){
            $this->data_set = $this->db->query("SELECT * FROM Players ORDER BY PlayerName ASC;");

            $table="<table><tr><th>Player ID</th><th>Player Name</th></tr>";

            while ($row = $this->data_set->fetch()){
                $select_list.="<tr><td>".$row['PlayerID']."</td><td>".$row['PlayerName']."</td></tr>";
            }

            $table.="</table>";
            return $table;

        }
    }else{
        $table="<table border=1><tr><th>Player ID</th><th>Player Name</th></tr>";
        $table.="</table>";
        return $table;
    }

}

I have no idea why but it's the if($this->data_set) portion. Works on one, fails on the other. Preceding statements are the exact same. Yet it will always just return my blank table in my else statement, or the giant error if I turn on exceptions.

Aucun commentaire:

Enregistrer un commentaire