NYCPHP Meetup

NYPHP.org

[nycphp-talk] Mysql question!

Rob Marscher rmarscher at beaffinitive.com
Tue Oct 31 11:16:23 EST 2006


"if exists" is a sql thing.  i.e. DROP TABLE tablename IF EXISTS; or 
CREATE TABLE tablename IF NOT EXISTS;

Here's one way to check if your mysql table doesn't exist with php:

if (mysql_query("DESCRIBE Customer_Info") === false)
{
    // create the table
}

FYI, there's a nyphp-mysql list that mysql-specific php questions can be 
sent to.
-Rob

tuon1 at netzero.net wrote:
>
> Hi, everybody!
>
> I want to ask you all to help me out in solving my database question.
>
> Here is what I want to accomplish:
>
> I want to add my users, one by one, to my Mysql database via a 
> registration form.
>
> When a user fills out a registration form, my script should gather all 
> the information
>
> and add them to the database. I pretty much got all that part working, 
> but I want my
>
> script to check to see whether a table exists or not. If it doesn't 
> exist, my script
>
> should create a table and then proceed to add the information to the 
> database;
>
> and if it does exist, then it should not create one and proceed to use 
> the existed table.
>
> 1) Is there a php function that take care of this job that I can use?
>
> I tried to use: if exists(mytablename)
>
> but, it doesn't work.
>
> Here is my code: (some codes are left out for illustration purpose.)
>
> <?php
> include("dbinfo.inc.php");
>   class TableInfo
>        {
>         function _construct()    //A Constructor
>            {
>             /* Initialize database */
>             mysql_connect($localhost,$username,$password);
>             @mysql_select_db($database) or die( "Unable to select 
> database");
>             if exists(!Customer_Info)  //Here it doesn't work, error!!!!
>                {
>                 $query = "CREATE TABLE Customer_Info
>                 (
>                  FirstNameCol VARCHAR(15) NOT NULL,
>                  LastNameCol VARCHAR(20) NOT NULL,
>                  AddressCol VARCHAR(40) NOT NULL,
>                  CityCol VARCHAR(20) NOT NULL,
>                  StateCol VARCHAR(20) NOT NULL,
>                  ZipCodeCol INT(5) NOT NULL,
>                  AreaCodeCol INT(3) NOT NULL,
>                  PhoneCol INT(15) NOT NULL,
>                  EmailCol VARCHAR(30) NOT NULL,
>                  LoginNameCol VARCHAR(20) NOT NULL,
>                  PasswordCol VARCHAR(20) NOT NULL
>                 )";
>                 mysql_query($query);
>                }
>
>         //Add new customer to database
>         function AddNewCustomer($FirstName, $LastName, $Address,
>                                 $City, $State, $ZipCode,
>                                 $AreaCode, $Phone, $Email,
>                                 $WebsiteURL, $LoginName, $Password
>                                )
>            {
>                 $query = 'INSERT INTO Customer_Info (FirstNameCol,
>            LastNameCol, AddressCol, CityCol, StateCol,
>                                 ZipCodeCol, AreaCodeCol, PhoneCol,
>                                 EmailCol, WebsiteURLCol,
>            LoginNameCol, PasswordCol
>                                )
>     VALUES ("'. $FirstName . '", "' . $LastName . '",
>                      "' . $Address . '", "' . $City . '",
>       "' . $State . '", "' . $ZipCode . '",
>       "' . $AreaCode . '", "' . $Phone . '",
>       "' . $Email . '",
>       "' . $WebsiteURL . '", "' . $LoginName . '",
>                      "' . SHA1($Password) . '")';
>   }
>
> Feel free to correct my code and give suggestions for better techniques.
>
> Thanks!
>
> Paul
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>   




More information about the talk mailing list