Select All from table if table exists

Codes here !

Moderators: macek, egami, gesf

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

Postby 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 Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Select All from table if table exists

Postby 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.

rida2
New php-forum User
New php-forum User
Posts: 3
Joined: Tue Aug 14, 2012 1:51 am

Re: Select All from table if table exists

Postby rida2 » Tue Aug 14, 2012 3:36 am

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.
2. Why $sql= "$sql . . . .twice- simplify- only once.
3. Do you have to set permissions for the table on the host?

Also try mysql at w3school.com
Good luck.

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

Re: Select All from table if table exists

Postby 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.


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest