jbradley04 Posted December 28, 2012 Share Posted December 28, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/ Share on other sites More sharing options...
DavidAM Posted December 28, 2012 Share Posted December 28, 2012 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!!!!!!!!!!!!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/#findComment-1401703 Share on other sites More sharing options...
jbradley04 Posted December 28, 2012 Author Share Posted December 28, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/#findComment-1401704 Share on other sites More sharing options...
Muddy_Funster Posted December 28, 2012 Share Posted December 28, 2012 I'm with DavidAM on this one. What your doing is nuts (on the surface of it at least). Why do you even want to think about doing this, never mind actualy doing it? Quote Link to comment https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/#findComment-1401767 Share on other sites More sharing options...
Christian F. Posted December 28, 2012 Share Posted December 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272434-while-loop-and-alter-mysql/#findComment-1401834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.