Jump to content

Adding Table Column


Ken2k7

Recommended Posts

Say I have this:

 

$query = "CREATE TABLE test(id mediumint(, name varchar(255), date varchar(20))";

mysql_query($query) or die(mysql_error());

 

So that table's created already. But I want to write a php script to change the already created table to this:

 

$query = "CREATE TABLE test(id mediumint(, name varchar(255), password varchar(255), date varchar(20))";

mysql_query($query) or die(mysql_error());

 

How would I do this without having to drop the table and then re-write the entire table again?

Link to comment
https://forums.phpfreaks.com/topic/82254-adding-table-column/
Share on other sites

Thank you.

 

Sorry to have to keep bumping this, but is there a way to check if a table row already exists before adding/removing it? Can this also check for a table column?

 

Examples:

- Remove table ROW `id` from table `member` ONLY if it exists

- Add table ROW `id` to table `member` ONLY if it DOESN'T exist

- Remove table COLUMN `gender` from table `member` ONLY if it exists

- Add table COLUMN `gender` to table `member` ONLY if it DOESN'T exists

 

Could someone give me examples on how I would do those 4 entries?

 

Thank you in advance,

Ken

Link to comment
https://forums.phpfreaks.com/topic/82254-adding-table-column/#findComment-422232
Share on other sites

Thank you.

 

Sorry to have to keep bumping this, but is there a way to check if a table row already exists before adding/removing it? Can this also check for a table column?

 

Examples:

- Remove table ROW `id` from table `member` ONLY if it exists

- Add table ROW `id` to table `member` ONLY if it DOESN'T exist

- Remove table COLUMN `gender` from table `member` ONLY if it exists

- Add table COLUMN `gender` to table `member` ONLY if it DOESN'T exists

 

Could someone give me examples on how I would do those 4 entries?

 

Thank you in advance,

Ken

 

 

mysql_query("UPDATE") or die(mysql_error());

 

will tell if you a row already exists.

Link to comment
https://forums.phpfreaks.com/topic/82254-adding-table-column/#findComment-422234
Share on other sites

mysql_query("UPDATE") or die(mysql_error());

 

will tell if you a row already exists.

So that returns a boolean?

 

So I would have to do something like:

<?php

$exists = mysql_query("UPDATE") or die(mysql_error());

if ($exists){
$sql = "ALTER TABLE `member` DROP COLUMN `id`";
mysql_query($sql, $this->db_connect) or die(mysql_error());
}

?>

 

I was thinking of something along the lines of:

<?php

$sql = "ALTER TABLE `member` DROP COLUMN IF EXISTS `id`";
mysql_query($sql, $this->db_connect) or die(mysql_error());

?>

Link to comment
https://forums.phpfreaks.com/topic/82254-adding-table-column/#findComment-422242
Share on other sites

There are specific MySQL commands like EXISTS, NOT EXISTS, and when used with IF can do much of what you are asking about. You can also do it 'long hand' using PHP hooks and checking num_rows or num_fields or whatever. The short answer is, there are many many ways of doing what you ask here... you need to absorb a bit more information is all. It appears you 'get it' to the point of where you're generating some code in your head, but you seem to be asking "will this work?" The answer is: test it and let us know! If you get an error and simply cannot figure out why the error is happening, then post the code with the error and we can help.

 

Just create a test database with test fields and values, and have at it. Part of learning is experimenting; you can't hurt anything.

 

Anyways, last minute holiday stuff going on here, so I gotta get moving along.

 

Good luck!

 

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/82254-adding-table-column/#findComment-422598
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.