vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Need to check for a column in a table (https://vborg.vbsupport.ru/showthread.php?t=237631)

TalkVirginia 03-06-2010 08:06 AM

Need to check for a column in a table
 
I'm doing an addon and I an install plugin I need to do the following in a mySQL query or a series of queries.

Psuedocode:

add a new column
check for an existing column from an old version of the addon.
If the old column exists
Move the data for all records to the new column

Anyone know if this is possible and if so how would I go about it?

Dylanblitz 03-06-2010 06:24 PM

Haven't tested it but this should work, just put it in your product xml install code and replace with the real variables.

PHP Code:

$db->query_write("ALTER TABLE " TABLE_PREFIX "TABLE_NAME ADD NEW_COLUMN mediumint(8) unsigned NOT NULL DEFAULT '0' AFTER AN_EXISTING_COLUMN");

$column_query $db->query_read("SHOW COLUMNS FROM " TABLE_PREFIX "TABLE_NAME LIKE 'OLD_COLUMN_NAME'");

while (
$column_data $db->fetch_array($column_query))
{
$db->query_write("UPDATE " TABLE_PREFIX "TABLE_NAME SET NEW_COLUMN = OLD_COLUMN_NAME");



TalkVirginia 03-06-2010 06:40 PM

Thanks! This looks a bit easier than what I was originally toying with. I was trying to do it with a table join to itself in an update query.

Something like:

UPDATE TABLE_NAME as T2
SET t2.NEW_COLUMN = t1.OLD_COLUMN
INNER JOIN TABLE_NAME T1
ON T2.UserID = T1.UserID
WHERE T2.UserID = @UserID

I just wasn't able to figure out how to loop it to get the userid.


All times are GMT. The time now is 09:34 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00926 seconds
  • Memory Usage 1,721KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete