Page 1 of 1

Inserting and populate new field

Posted: Tue Aug 03, 2004 8:45 pm
by CyberGarage
I have a site that is running on PHP/MySQL. I need to add a new field to a table. This is no problem, but I want to automatically populate this field in all records in the table. The value will be the same for every record, FL. I could go through manually and do this but there are 800 records to do. Can anyone help me with this.

Thanks,
Mark

Posted: Tue Aug 03, 2004 8:59 pm
by nigma
Make your script first add the field to the table, then use a second query to make the new fields value "FL"

Need help with the queries?

Posted: Tue Aug 03, 2004 9:43 pm
by CyberGarage
nigma,

Thanks for the input but unfortunately I am new to both PHP and MySQL so I don't know how to create the queries. Any help you could give would be appreciated.

Thanks,
Mark

Posted: Tue Aug 03, 2004 9:53 pm
by nigma
Absolutely CyberGarage.

To query a mysql database:

Code: Select all

<?php
// mysql_connect() establishes a connection with a mysql server
mysql_connect("hostname", "username", "password");

// mysql_select_db() selects a mysql database
mysql_select_db("database");

// mysql_query() executes a mysql query
// the first query will add the field 'field' to the table 'table'
// the second query will insert the value "FL" into the newly added field
mysql_query("alter table 'table' add 'field' char(2)");
mysql_query("update 'table' set 'field'='FL'");
?>
I would suggest reading the following man pages:
http://us2.php.net/manual/en/function.mysql-connect.php
http://us2.php.net/manual/en/function.m ... ect-db.php
http://us2.php.net/manual/en/function.mysql-query.php

And maybe check out the following tutorials:
Collection of PHP Tutorials - http://www.onlamp.com/pub/ct/29
Intro to PHP - http://www.w3schools.com/php/default.asp
MySQL - http://devshed.spunge.org/Server_Side/M ... page1.html
PHP with MySQL - http://www.onlamp.com/pub/a/php/2004/02 ... tions.html

Let me know if you need some more help or if i've made a mistake.

Posted: Tue Aug 03, 2004 11:32 pm
by CyberGarage
nigma,

Thanks again for your help. I will try this tomorrow. I will also read the manual pages and tutorial you recommended. This is why I come to this site. The helpful individuals like yourself.

Thanks,
Mark