Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Select All from table if table exists

Codes here !

Moderators: macek, egami, gesf

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
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

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


Return to mySQL & php coding

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.