Select All from table if table exists

Codes here !

Moderators: macek, egami, gesf

Post Reply
MikeSpider
New php-forum User
New php-forum User
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Select All from table if table exists

Post by MikeSpider » Sun Aug 12, 2012 7:45 am

Hi guys i'm trying to add the 'exist' clause in my query so that it does not throw an error if the table doesn't exist.
items from the table are presented on page load, however if the user is new and has not created any table, it says that "table does not exist".

Code: Select all

...
$pjtTbl = $user . "_" . $project_name;

$sql = " $sql = "SELECT * FROM $pjTbl";
            $result = $this->db->mysqli->query($sql);      
...      
        
Thanks in advance,
Mike

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 594
Joined: Fri Aug 05, 2011 9:53 am

Re: Select All from table if table exists

Post by NigelRen » Sun Aug 12, 2012 11:40 pm

You would have to check if the table exists by using the information schema ( for mysql ) or other system catalogues to handle it gracefully. Or you could try a select and simply trap the error, at the base level you could just assume any error meant the table didn't exist.

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 594
Joined: Fri Aug 05, 2011 9:53 am

Re: Select All from table if table exists

Post by NigelRen » Tue Aug 14, 2012 11:31 am

rida2 wrote:A novice's thought:
1. the name of your table you SELECT FROM should be just a name - not a variable (ie not $) for some systems.
This would be replace by the value of the variable when PHP evaluates the expression
rida2 wrote:3. Do you have to set permissions for the table on the host?
You always need permissions to a table at least read permissions - the information schema uses views to give database information. Although if you don't have access permissions to a table - being able to select the data wouldn't work either.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest