Jump to content

Recommended Posts

Hello,

I am trying to do a while loop and the loop does fine because I tested it using an echo statement.

However, Every time I remove the echo statement and place a MySQL Alter in there, it does not work.

 

$d = 1;
while ($d <= $depth)
{
$w = 1;
while ($w <= $width)
{
mysql_query("ALTER TABLE `$b` ADD `test$b$w$d` int unsigned not null");
$w++;
}
$d++;
}

 

But.... When I use the same Alter Phrase outside the loop it works fine.

 

What am I missing here? I do not understand why this will not work in the loop but does outside of it!

 

Thanks for your help!

Link to comment
https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/
Share on other sites

The concept of altering a table in a loop is soo scary and soo crazy that I hesitate to even wade into this murky swirling pool ... but what's life without a little adventure, eh?

 

Question: What are your values for $depth and $width? If they both go into double digits (or higher, you are going to run into duplicate column names here. When $w = 1 and $d = 11, column name = testb111. Then when $w = 11 and $d = 1, column name = testb111 ... oh, look, that's the same name!

 

If you are seriously creating a table this way,

YOU REALLY NEED TO RE-THINK YOUR DATABASE DESIGN!!!!!!!!!!!!!!!

IT IS WRONG!!!!!!!!!!!!!!!!!!!

I actually was not going to name it that. I was just using that as an example and you are absolutely correct! Which I never thought about...

Anyways, I need to add these fields to a table and it is dependant on a value from a query.

So, $w could = 2 or 20 and $d the same!

The field would be field$w_$d so they should never duplicate....

Any thoughts how to make this work?

Appreciate the help

If you tell us why you think you need this "solution", what you're trying to make with it, then I'm sure we can offer you a far better solution to your problem.

I've done quite a bit of work with dynamic database designs, but I've never even dreamed of doing something like this; It's just way too far out there, and almost destined to be a major headache in the future when you're going to maintain it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.