-5

Hi I'm trying to create some temporary tables in mysql using php

my code:

<?php
mysql_connect("localhost","root","sahan");//database connection
mysql_select_db("callcenter");

$maketemp="create table #temp1 (id varchar(50),date varchar(50),csc varchar(50),effectedareas varchar(50),agent varchar(50))";

$data1 = mysql_query($maketemp)
 or die(mysql_error());

?>

but it gives an 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 '' at line 1"

My MySQL version is as below

MySQL Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i686) using readline 5.1

I'm not much fluent in php coding please can some one help me??

bummi
  • 27,123
  • 14
  • 62
  • 101
  • 3
    [MySQL Temporary Tables](http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm) – bummi Sep 23 '14 at 05:33

3 Answers3

1

I do not know why you want a table name like that but you need indentifier quotes (backticks) for it to work.

`#temp1`

You need quotes or else it will considered a comment. #

$maketemp = "CREATE TABLE `#temp1` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `date` varchar(50),
    `csc` varchar(50),
    `effectedareas` varchar(50),
    `agent` varchar(50),
    PRIMARY KEY (`id`)
) COLLATE = 'utf8_general_ci'
";

Please refrain from using mysql_* functions, they are deprecated. Click here.

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

Here problem is declaring varchar for primay key with auto increment. So make id datatype to int. then its working

$maketemp = "CREATE TABLE `#temp1` (
    `id` VARCHAR(50) NOT NULL AUTO_INCREMENT,
    `date` varchar(50),
    `csc` varchar(50),
    `effectedareas` varchar(50),
    `agent` varchar(50),
    PRIMARY KEY (`id`)
) COLLATE = 'utf8_general_ci'
";

If you want to create temporary table, In my sql, there is temporary table functionality as http://www.mysqltutorial.org/mysql-temporary-table/

Vamsi Krishna
  • 161
  • 10
0

Taken from MySQL Temporary Tables
Temporary tables were added in MySQL version 3.23. If you use an older version of MySQL than 3.23, you can't use temporary tables, but you can use heap tables.

You are mixing up SQLServer syntax with MySQL syntax. While you are able to create a temporary table in SQLServer with #table or a gloabal temporary table with ##table in MySQL you have to use the keyword TEMPORARY

CREATE TEMPORARY TABLE temp1 (
         id varchar(50)
         ,date varchar(50)
         ,csc varchar(50)
         ,effectedareas varchar(50)
         ,agent varchar(50)
         )
bummi
  • 27,123
  • 14
  • 62
  • 101
  • thank you bummi, this code works on mysql of course. but in php it doesnt work. it gives an error – user3916665 Sep 23 '14 at 06:41
  • I'm not familiar with PHP so I can just assume that the link provided by @Ghost [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) might answer the PHP part of your question. – bummi Sep 23 '14 at 06:49