MySQL
- Started
- Last post
- 11 Responses
- cosmo
trying to update a table structure using php, i know the mysql syntax should be right. Can anyone see an error?
$sql = 'ALTER TABLE `tablename` ADD `field` VARCHAR(255) NOT NULL';
- gabriel20
are 'tablename' and 'field' not actually surrounded by single quotes in your code?
- Anarchitect0
yeah, lose the single quotes.
- cosmo0
tried that too. didn't work.
- gabriel20
what error is it giving you? Do you have access to PHPMyAdmin somewhere? I find the SQL tool in that invaluable when trying to sort out mysql errors.
- industry730
You need a DEFAULT Value since you are specifying NO NULL
ALTER TABLE `table_name` ADD `column_name` varchar(255) NOT NULL DEFAULT "default_value"
- cosmo0
yeah i have phpmyadmin, but building this front end for work. Don't know why they want this crap.
I took the single quotes off too, didn't work. Statement basically dies.
- gabriel20
try mocking the table up in phpmyadmin then run the script and see what error it gives you. you shouldn't need to set a default value.
- cosmo0
yup didn't think you need to set default value.
- gabriel20
also make sure you're not using a reserved word for the field name:
- cosmo0
Worked like a charm I ran the sql on phpmyadmin.
$sql_update = "ALTER TABLE $table_name ADD $column_name INT(255) NOT NULL";
$result = mysql_query ($sql_update) or die ("Error: Could update table structure of table: " . $table_name . ". " . $sql_update);
- cosmo0
look got it to work. stupid me had another single quote at the end. hehe thanks.