Automating creating tables in php for mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markjm
Forum Newbie
Posts: 4
Joined: Tue Feb 03, 2004 6:26 am

Automating creating tables in php for mysql

Post by markjm »

Hello,

I've been trying to get code to work which will have php create a
mysql table by looping through an array, does anyone have any ideas as to where I'm going wrong. Here's my code:

for ($n = 0; $n < 29; $n++){
$my_array[$n] = "MyText" . $n . " tinytext,";
}

$create_tab = "create table repeats(
contactID smallint not null auto_increment,
lastName tinytext,
for ($n = 0; $n < 29; $n++) $my_array[$n]
caption tinytext,
contactType char(1),
primary key (contactID))";
echo "Table contacts was ".
(mysql_query ($create_tab) ? "" : " NOT").
" created successfully<br>";

Thanks in advance,
Mark M
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

dunno. i had the same problem, exausted it, and then just eventually manually entered in the values myself.

however, maybe try something like this... i can't test it as i dont' have a pc with php that works, so if it doesn't don't be too harsh ;)

Code: Select all

for ($n = 0; $n < 29; $n++)
{ 
   $my_array[] = "MyText" . $n . " tinytext,"; 
} 


$create_tab = "create table repeats( 
contactID smallint not null auto_increment, 
lastName tinytext, ".
for ($n = 0; $n < 29; $n++) 
{
   $my_array[$n];
}
."caption tinytext, 
contactType char(1), 
primary key (contactID))"; 
echo "Table contacts was ". 
(mysql_query ($create_tab) ? "" : " NOT"). 
" created successfully<br>";
as you can see, all i did was concatinate the for loop instead of trying to apply it within the actual create statement call within your original code.. also took out $n for the first declaration values of $my_array.

hope this helps/works
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

for ($n = 0; $n < 29; $n++){
  $my_array[$n] = 'MyText' . $n . ' tinytext';
}

$create_tab = 'create table repeats(
contactID smallint not null auto_increment,
lastName tinytext,'.join(',', $my_array).
', caption tinytext,
contactType char(1),
primary key (contactID))';
echo "Table contacts was ".(mysql_query ($create_tab) ? "" : " NOT")." created successfully<br>";
... works for me :o
markjm
Forum Newbie
Posts: 4
Joined: Tue Feb 03, 2004 6:26 am

Thanks Infolock and Markl999

Post by markjm »

Thanks for the help :P ,
I tried both sets of code. The first bit gave me a parse error,
I then tried Mark1999 code and it worked a treat,

thanks a lot

I'm over the moon

Mark M


Here's the code again:

Code: Select all

<?php


for ($n = 0; $n < 29; $n++){ 
  $my_array[$n] = 'MyText' . $n . ' tinytext'; 
} 

$create_tab = 'create table repeats( 
contactID smallint not null auto_increment, 
lastName tinytext,'.join(',', $my_array). 
', caption tinytext, 
contactType char(1), 
primary key (contactID))'; 
echo "Table contacts was ".(mysql_query ($create_tab) ? "" : " NOT")." created successfully<br>"; 

?>
Post Reply