4 Replies - 435 Views - Last Post: 15 May 2011 - 12:39 PM Rate Topic: -----

#1 _Umar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 24-December 08

Create table query won't create table with an integer as its name

Posted 15 May 2011 - 12:09 PM

Hi,
Well the title says it all,
I have a query which creates a table in the sql database if the table with the 'received' name doesn't already exist,
if I send a table name like 'abc' or 'customer' or '123abc' , it will create the table with the given details,no error, no foul
But if the table name sent is an 'integer' e.g '500' or '10-500' then it gives me this error
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 '500 (First_Name char(50), Last_Name char(50)' at line 1




This is my function which check and creates the table if it doesent exist:
function create_student_table($id_recieved) 
{

global $student_id_table_returned;
$result = mysql_query("SHOW TABLES FROM fyp_project_student");

while ($row = mysql_fetch_row($result)) {
    echo "<br/><B>Table: {$row[0]}\n</b>";
    $student_id_table_returned = $row[0];   
}
	echo "<br/>". $id_recieved;

else 
{
	mysql_select_db("fyp_project_student");
	$qur = mysql_query("CREATE TABLE $id_recieved
	(First_Name char(50),
	Last_Name char(50),
	Address char(50),
	City char(50),
	Country char(25),
	Birth_Date date)
	");    
    
	if(!$qur) { echo "<h2>fail</h2>" . mysql_error();}
	else
	echo "<br/>Student table " . $id_recieved . " has been created";	
}

}



$id_received will have the student id (table name) to be checked
And I want it accept numbers because I will be sending student ID to the function to create the table using it as is the table name, and ID wil always be a number with hyphens e.g 10-33432-234

I have also tried pasting this direct into MYSQL query tab,
still got the same error.
So my question is that why is it accepting a character but not a interger as a table name.


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Create table query won't create table with an integer as its name

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Create table query won't create table with an integer as its name

Posted 15 May 2011 - 12:24 PM

How about creating a table with a MD5 hash of the student id?

...
else 
{
	$table_name = md5($id_recieved);
	mysql_select_db("fyp_project_student");
	$qur = mysql_query("CREATE TABLE $table_name
	(First_Name char(50),
	Last_Name char(50),
	Address char(50),
	City char(50),
	Country char(25),
	Birth_Date date)
	"); 
...   
    

Was This Post Helpful? 1
  • +
  • -

#3 _Umar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 24-December 08

Re: Create table query won't create table with an integer as its name

Posted 15 May 2011 - 12:33 PM

View Postcodeprada, on 15 May 2011 - 12:24 PM, said:

How about creating a table with a MD5 hash of the student id?


Hi,
yes it actually worked, Thanks.
So this problem is solved, but do you still see any reason, why its not accepting integers through query?
because to be honest this is a little unusual to happen.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Create table query won't create table with an integer as its name

Posted 15 May 2011 - 12:36 PM

I would believe it's an invalid table name but I'm not sure what makes it invalid. You could refer to the guys in the MySQL forum about that issue since it's actually a database problem.
Was This Post Helpful? 1
  • +
  • -

#5 _Umar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 24-December 08

Re: Create table query won't create table with an integer as its name

Posted 15 May 2011 - 12:39 PM

Ohh, how did database section missed my eye , I'll go and ask there.
Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1