Connecting Tech Pros Worldwide Help | Site Map

How to create a table from PHP?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:45 PM
mh
Guest
 
Posts: n/a
Default 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="CREATE 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.


  #2  
Old July 17th, 2005, 07:35 PM
Andy Hassall
Guest
 
Posts: n/a
Default Re: How to create a table from PHP?

On 17 Jul 2005 09:42:34 -0700, "mh" <marchenri1@gmail.com> wrote:
[color=blue]
>$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.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
  #3  
Old July 17th, 2005, 10:05 PM
mh
Guest
 
Posts: n/a
Default 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@gmail.com> wrote:
>[color=green]
> >$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="CREATE 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?

  #4  
Old July 18th, 2005, 12:55 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default 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@gmail.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="CREATE 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@attglobal.net
==================
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.