Forum Moderators: coopster

Message Too Old, No Replies

Using loop to ALTER a table

         

Marcellus

12:57 am on Jan 17, 2006 (gmt 0)



Hiya, bit of a noob here but I just made this script & I can't figure out why it fails.

I've created a database and a table manually. Now, I wish to add the COLUMNS automatically through the use of a double loop. There will be 'x' and 'y' components (ie: rXdY) to the column names, thus the use of the loops.

My problem lies in execution of the query I think. Syntactically it looks ok, but I error out during the execution of mysql_query.

Any ideas...anyone? Would an array be a better idea here?

Thx for reading this far...and triple so if you can help :D

<?php
include "../common_db.inc";
error_reporting(0);
$link_id = db_connect("labbrite");

if(!$link_id) die(sql_error());
else echo "Successfully made connection to $dbhost.<br>";

$query = "ALTER TABLE drawings ADD $combo VARCHAR(8) NOT NULL";

for ($x = 1; $x < 10; $x++){
for ($y = 1; $y < 10; $y++){
$combo = "r$x" . "d$y"; //just here to help 'visualize'
echo "ALTER TABLE drawings ADD $combo VARCHAR(8) NOT NULL <br>"; //same, just here to 'see'

mysql_query($query) or die('Error, query failed');
}
}
?>

ergophobe

8:03 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld Marcellus.

You are creating your query before you define combo. Put the line with $query= below the echo so you know they are the same, and then see what happens.

Also, look in the forum charter [webmasterworld.com] I wrote a post to help folks get started on debugging. I think it will be helpful to you at this point (look for a link to something titled Troubleshooting 101).