How to create a table from PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mh

    How to create a table from PHP?

    Hello,

    I'm trying to create a table from a PHP script.[color=blue]
    >From what I read in my book about PHP and MySQL I[/color]
    should do something like that:

    $sql_query="CRE ATE TABLE '$num' (
    'variable1' mediumint(6) unsigned NOT
    NULL,

    'variable2' smallint(6) unsigned NOT NULL,
    PRIMARY KEY(numero),
    )";
    First I tried without the 'primary key' and the '' for
    the names of the variables. My script was not creating
    the table so I decided to add the '' and the "primary
    key". It was still not working. Then I've found that
    this is working:

    $sql_query = 'CREATE TABLE $num ('
    . ' `variable1` DATE NOT NULL'
    . ' )';

    I'd like to know why my first test was not working
    (cause it is similar to a listing in a book) and if I
    really need (it's related) to use the '.' between each
    line of my command?

    Another problem is that the table is created with the
    name $num and is not replaced like I want by its
    value. How can I solve this other problem?

    Thanks a lot for your help.

  • Andy Hassall

    #2
    Re: How to create a table from PHP?

    On 17 Jul 2005 09:42:34 -0700, "mh" <marchenri1@gma il.com> wrote:
    [color=blue]
    >$sql_query="CR EATE TABLE '$num' ([/color]

    Don't put quotes around identifier names. If you must, then in MySQL use
    either ` or ", but not '. ' is for strings, not identifiers.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • mh

      #3
      Re: How to create a table from PHP?

      Hello,

      Andy Hassall a écrit :[color=blue]
      > On 17 Jul 2005 09:42:34 -0700, "mh" <marchenri1@gma il.com> wrote:
      >[color=green]
      > >$sql_query="CR EATE TABLE '$num' ([/color]
      >
      > Don't put quotes around identifier names. If you must, then in MySQL use
      > either ` or ", but not '. ' is for strings, not identifiers.[/color]
      Ok, but I did that I think. I said in my message that I also tried like
      that:
      $sql_query = 'CREATE TABLE $num ('
      . ' `variable1` DATE NOT NULL'
      . ' )';
      In this case I don't use the quotes around the name of the table but
      MySQL still creates a table called $num and doesn't replace the
      variable $num but its value like I wish.

      I also tried:
      $sql_query="CRE ATE TABLE $num (variable DATE NOT NULL)";
      but without success.
      I feel that I tried every possible combiantion without success. There
      must be a solution. But what is it?

      Comment

      • Jerry Stuckle

        #4
        Re: How to create a table from PHP?

        mh wrote:[color=blue]
        > Hello,
        >
        > Andy Hassall a écrit :
        >[color=green]
        >>On 17 Jul 2005 09:42:34 -0700, "mh" <marchenri1@gma il.com> wrote:
        >>
        >>[color=darkred]
        >>>$sql_query=" CREATE TABLE '$num' ([/color]
        >>
        >> Don't put quotes around identifier names. If you must, then in MySQL use
        >>either ` or ", but not '. ' is for strings, not identifiers.[/color]
        >
        > Ok, but I did that I think. I said in my message that I also tried like
        > that:
        > $sql_query = 'CREATE TABLE $num ('
        > . ' `variable1` DATE NOT NULL'
        > . ' )';
        > In this case I don't use the quotes around the name of the table but
        > MySQL still creates a table called $num and doesn't replace the
        > variable $num but its value like I wish.
        >
        > I also tried:
        > $sql_query="CRE ATE TABLE $num (variable DATE NOT NULL)";
        > but without success.
        > I feel that I tried every possible combiantion without success. There
        > must be a solution. But what is it?
        >[/color]

        Look at strings in the PHP manual.

        When enclosed in single quotes, variable substitution does NOT take place - so
        your table name becomes $num.

        When you enclose a string in double quotes, PHP will substitute variables. So
        you should have something like:

        $sql_query = "CREATE TABLE $num ("
        ^ ^

        Note double quotes instead of single quotes. See
        http://www.php.net/manual/en/language.variables.php.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...