SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    california
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can't create a table in mysql through PHP?

    Is this the proper way to syntax creating a table through a PHP query?

    $query = "CREATE TABLE ".$ad_id." (offer_id SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, user_id SMALLINT(10) UNSIGNED NOT NULL, comments TEXT, date TIMESTAMP NOT NULL, PRIMARY KEY (offer_id))";

    the part I'm most concerned with is the ".$ad_id." portion. I'm pretty sure all the rest of it works because I tested the query in phpmyadmin with a name substituted for $ad_id. $ad_id is supposed to be a number by the way, if that changes anything. Thanks

  2. #2

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Backticks
    PHP Code:
    $query "CREATE TABLE `$ad_id` (offer_id SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, user_id SMALLINT(10) UNSIGNED NOT NULL, comments TEXT, date TIMESTAMP NOT NULL, PRIMARY KEY (offer_id))"

  3. #3
    SitePoint Zealot CdeMky's Avatar
    Join Date
    Sep 2004
    Location
    United States
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by drzoid
    Backticks
    PHP Code:
    $query "CREATE TABLE `$ad_id` (offer_id SMALLINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, user_id SMALLINT(10) UNSIGNED NOT NULL, comments TEXT, date TIMESTAMP NOT NULL, PRIMARY KEY (offer_id))"

    Out of curiosity, what's the difference between using backticks, and using an apostrophe?

    "CREATE TABLE '$ad_id' (offer_id ........etc. "

    instead of,

    "CREATE TABLE `$ad_id` (offer_id ........etc. "

    They seem to work equally well for me.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It only matters if you intend to create a table name with a MySQL reserved keyword.

  5. #5
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by HardCoded
    It only matters if you intend to create a table name with a MySQL reserved keyword.
    and it matters if you create a table name where

    Quote Originally Posted by chill2k66
    $ad_id is supposed to be a number
    which is IMHO not the best idea. Try to use table names where you don't have to care about whether you have to use backticks or not.

    A name may start with any character that is legal in a name. In particular, a name may start with a digit; this differs from many other database systems! However, an unquoted name cannot consist only of digits.
    see the manual
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •