Can anyone tell me why the following code works just fine......

$sql = "ALTER TABLE drivers ADD COLUMN a1 VARCHAR (20)";
$result = mysql_query($sql);

but this code doesnt.....

$sql = "ALTER TABLE drivers ADD COLUMN 1 VARCHAR (20)";
$result = mysql_query($sql);

As you can see I am trying to add a column which has a name of a number (1 in the above code).
I dont recieve any error messages. The top example adds the column just fine, and the 2nd example just does nothing.
I know it is ok to use numbers to name a column, as I have done it using the MySQL Control Center utility.

Any ideas?

Thanks

    this worked fine on my mysql command-line client.

    "alter table drivers add column 1 varchar (20);"

      I tried it and got the same results as gsf600y. I didn't think you could use numbers for names or names that start with numbers. I figured that column naming would conform to the same rules as naming databases and tables in that
      - Names may be constructed fom alphanumeric characters in the current character set, as well as the underscore and dollar('_' and '$').

      • Names may be up to 64 characters long.

      ( last two notes from "MySQL" by Paul DuBois and published by New Riders)

        It's possible, but i don't know if it is somehow confusing.

        i tried this lines, this time from php and not from mysql client;

        $query="update visitas set 1 = 'hello' where mes=6";
        $query="alter table visitas add column 2 varchar (20)";

        [FONT=courier new]
        mysql> select * from visitas;
        +-----+------+---------+------+-----------+------+
        | mes | ano | visitas | a1 | 1 | 2 |
        +-----+------+---------+------+-----------+------+
        | 1 | 2003 | 200 | NULL | NULL | NULL |
        | 2 | 2003 | 450 | NULL | NULL | NULL |
        | 3 | 2003 | 520 | NULL | something | NULL |
        | 4 | 2003 | 425 | NULL | NULL | NULL |
        | 5 | 2003 | 56 | NULL | NULL | NULL |
        | 6 | 2003 | 5 | NULL | hello | NULL |
        | 7 | 2003 | 680 | NULL | NULL | NULL |

        [/FONT]

          Ah.....just worked out what i was doing wrong.....

          I was trying:
          '1'
          but should have put:
          1

          Note the ` instead of ' 🙂

            yes! that makes the trick... sincerely, i don't know why.

            😃 😃 😃

              what is the difference between ' and ` ? any one wanna explain a bit...

                yes...
                i also have noticed that phpmyadmin always put tables around so there must be a logical explanation.

                  Write a Reply...