0

i want to create a table in the database using php (mysql_querry)

Table is having 'n' number of attributes. out of n, n-2 attribute names are available in the array.

I can't explain where i am getting the array but it looks like this- https://i.sstatic.net/tH98f.png

Here is the code for generating String to execute in mysql_querry.

    $str="CREATE TABLE $register_name(id int NOT NULL AUTO_INCREMENT, date DATE, ";
    $j=0;
    while($j<$i)
    {
        $str=$str.$roll_no[$j]." int(100), ";
        $j++;

    }
    $str=$str."PRIMARY KEY(id))";


require('blogic.php');
$obj = new blogic();
$createtable=$obj->create($str);

When i echo the $str, I get this:

CREATE TABLE $register_name(id int NOT NULL AUTO_INCREMENT, date DATE, 913310128 int(100), 0913310129 int(100), PRIMARY KEY(id))

However, it is giving error like this

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '913310128 int(100), 0913310129 int(100), PRIMARY KEY(id))' at line 1

I don't understand the problem. when i don't use roll_no array, it works fine.. Please let me know what is the problem in this.

1
  • I have never seen mysql field names beeing a number like 913310128, check if this is allowed. Consider to change it to something like i_ 913310128 Commented Mar 26, 2013 at 18:16

1 Answer 1

1

From the docs:

"Identifiers may begin with a digit but unless quoted may not consist solely of digits."

So, you could just quote the name:

$str=$str."`".$roll_no[$j]."` int(100), ";

Or, prefix it with a letter:

$str=$str."c".$roll_no[$j]." int(100), ";
Sign up to request clarification or add additional context in comments.

4 Comments

I think the quotes need to be backquotes.
You tried it with the backticks or the single quotes that Russell originally put in his answer?
If you didn't try what I wrote, why did you say it didn't work?
Giving columns numeric names is just asking for trouble. Put a letter in front, like Russell's second suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.