vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   SQL Query - Check Existance of Data/Column before making changes. (https://vborg.vbsupport.ru/showthread.php?t=132031)

DaNIEL MeNTED 11-20-2006 02:19 PM

SQL Query - Check Existance of Data/Column before making changes.
 
Hi Guys... I have 2 questions.

1) During an install I'm using "CREATE TABLE IF NOT EXISTS" to create tables but there's no similar option for ALTER TABLE so what is the best way to check if a column already exists in the users table?

2) During the same install I'm using INSERT INTO to populate a table... how do I check if the table is empy first?

THX!

Right now I'm just ignoring errors using $vbulletin->db->hide_errors(); but I'd rather do a proper check.

Paul M 11-20-2006 02:24 PM

Quick answers ;

1. Use DESCRIBE table and loop through the results looking for the fieldname.

2. Use SELECT * FROM table LIMIT 1 If the number of rows returned = 1 then you have records in the table.

DaNIEL MeNTED 11-20-2006 02:28 PM

[high]* DaNIEL MeNTED wishes the quick answers were long. :confused:
[/high]

Off to go look for code.

Hmm... found and modified this for columns. Is there a cleaner way to do it?

PHP Code:

$query $vbulletin->db->query("SHOW COLUMNS FROM " TABLE_PREFIX "table LIKE 'column'") or die(mysql_error());

if (
mysql_num_rows($query) == 0) { insert column 

And for rows in a table:

PHP Code:

$query $vbulletin->db->query("SELECT * FROM  " TABLE_PREFIX "table LIMIT 1") or die(mysql_error()); 

if (
mysql_num_rows($query) == 0) {   add rows 

Make sense?


All times are GMT. The time now is 04:31 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.02273 seconds
  • Memory Usage 1,713KB
  • 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
  • (2)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