User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 455,990 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,762 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4536 | Replies: 2
Reply
Join Date: Dec 2007
Posts: 14
Reputation: webguru07 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
webguru07 webguru07 is offline Offline
Newbie Poster

check if mysql table exists

  #1  
Dec 5th, 2007
is there a way that i can check to see if a mysql table exists and if not add the table to the database with a session_id as a table name
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 416
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 3
Solved Threads: 81
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro in Training

Re: check if mysql table exists

  #2  
Dec 5th, 2007
Try this. I haven't tested it but it should do the trick. The key is to use the @ sign on line 2 to prevent the error message from showing up on the clients browser. Also, if you add or die(mysql_error()) to the end of line 2, the script will stop.
  1. $sql="SELECT * FROM $table";
  2. $result=@mysql_query($sql);
  3. if (!$result)
  4. {
  5. echo "No table exists";
  6. // Create a MySQL table
  7. $tblname=$_SESSION['ID'];
  8. mysql_query("CREATE TABLE $tblname(
  9. id INT NOT NULL AUTO_INCREMENT,
  10. PRIMARY KEY(id),
  11. name VARCHAR(30),
  12. age INT)")
  13. or die(mysql_error());
  14. echo " so I created one!";
  15. }
Reply With Quote  
Join Date: Aug 2007
Location: Argentina
Posts: 83
Reputation: martin5211 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
martin5211's Avatar
martin5211 martin5211 is offline Offline
Junior Poster in Training

Re: check if mysql table exists

  #3  
Dec 5th, 2007
Other suggestion:

CREATE TABLE IF NOT EXISTS `session_id`
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC