Page 1 of 1
Automating creating tables in php for mysql
Posted: Tue Feb 03, 2004 6:26 am
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
Posted: Tue Feb 03, 2004 5:12 pm
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
Posted: Tue Feb 03, 2004 5:22 pm
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

Thanks Infolock and Markl999
Posted: Wed Feb 04, 2004 3:58 am
by markjm
Thanks for the help

,
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>";
?>