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
Automating creating tables in php for mysql
Moderator: General Moderators
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
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
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>";hope this helps/works
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>";Thanks Infolock and Markl999
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:
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>";
?>