TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 07-04-2008, 12:51 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 61
Thanks: 49
Dave is on a distinguished road
Default Updating multiple columns in a single record

I have a table with about 400 fields, but there is a series of fields entitled n1, n2...n50. IOW, there are 50 "consequtively named" fields.

I need to replace all 50 of these fields with a NULL value in one record.

What is the most efficient way to do this? I understand the SQL UPDATE statement, but was kinda hoping that there is a looping technique that I could use, somehow...(?)

Thanks for any help...

Dave
Dave is offline  
Reply With Quote
Old 07-04-2008, 02:28 AM   #2 (permalink)
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 243
Thanks: 5
Kalle is on a distinguished road
Default

Is there any unique identifier with each row? eg. a primary key, unique key ect.?
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 07-04-2008, 03:10 AM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 61
Thanks: 49
Dave is on a distinguished road
Default

Hi, Kalle --

Yes, there is a unique identifying number for each row in the table.

Dave
Dave is offline  
Reply With Quote
Old 07-04-2008, 12:36 PM   #4 (permalink)
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 243
Thanks: 5
Kalle is on a distinguished road
Default

Hey

As far as I know theres no command to "updte all the rows" like you can select all rows in a statement.

So you need to do something like:

PHP Code:
$sql 'UPDATE `table` SET `n1`, `n2`, ..., `n50` = NULL WHERE `id` = \'' $something_unique '\' LIMIT 1;'
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
Dave (07-04-2008)
Old 07-04-2008, 12:49 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 61
Thanks: 49
Dave is on a distinguished road
Default

Thanks, Kalle --

Just for me to be specific, in the UPDATE query I would have to literally type the name of each of the 50 columns (n1, n2, n3, etc.) in order to set them all to NULL.

Is that correct?

Again, thank you...

Dave
Dave is offline  
Reply With Quote
Old 07-04-2008, 01:11 PM   #6 (permalink)
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 243
Thanks: 5
Kalle is on a distinguished road
Default

Yea thats what it looks like, you can ofcourse generate it in PHP if its names like "n[n]" where [n] is a number like:

PHP Code:
function generate_update_rows($value NULL)
{
    
$compiled_stack = Array();

    
/* Start number: 1 - End number 50 */
    
$times 50;

    for(
$n 1$n < ($times 1); ++$n)
    {
        
$compiled_stack[$n] = '`n' $n '`' . (!is_null($value) ? ' = \'' addslashes($value) . '\'' '') . (($times 1) != $n ', ''');
    }

    return(
implode(' '$compiled_stack));

And in your query just have like:
PHP Code:
$sql 'UPDATE `table` SET ' generate_update_rows('NULL') . ' LIMIT 1;'
__________________

Last edited by Kalle : 07-04-2008 at 05:27 PM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 07-04-2008, 05:25 PM   #7 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 61
Thanks: 49
Dave is on a distinguished road
Default

Wow, Kalle, thank you so much!

Yes, that is what I was looking for. Now it will take me some time to absorb what you have written, though I do get the gist of it right away.

I'm sure I'll have a question or two as I attempt to implement your code.

Dave
Dave is offline  
Reply With Quote
Old 07-04-2008, 05:27 PM   #8 (permalink)
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 243
Thanks: 5
Kalle is on a distinguished road
Default

I just updated my code, there was an error with comma's :)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 07-04-2008, 06:57 PM   #9 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 61
Thanks: 49
Dave is on a distinguished road
Default

I think it is bad luck to read your code a 2nd time!

Thanks again,
Dave
Dave is offline  
Reply With Quote
Old 07-12-2008, 06:22 PM   #10 (permalink)
The Contributor
 
Join Date: Apr 2005
Posts: 29
Thanks: 0
Dr John is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
I have a table with about 400 fields, but there is a series of fields entitled n1, n2...n50. IOW, there are 50 "consequtively named" fields.
Sounds like you have an unnormalised database...

Without seeing the field list and all the business logic, it's hard to normalise things for you of course, but if you have a number of columns called n1, n2, n3, etc, I'd guess that you should be normalising these into another table, with the main table's unique id as a foreign key in the new table.

One of the classic examples of unnormalised data that can lead to multiple columns like this is a person and their children
this is the wrong way to do it.
person (id, name, childname1, childname2, childname3, childname4, childname5, childname6....childnameN}

the correct way is like this
person {id, name,}
child (personID, childname)

variations on this:-
person (id, name)
teleNo (personID, tnumber)

business (id, name)
store (businessID, location)

movie (id, name)
actor (movieID, name)

and so on.

So, revisit your database design now, before you get too far into this problem, as it sounds like the design is wrong.
__________________
www.kidneydialysis.org.uk
Dr John is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:25 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0