Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 06-06-2008, 09:55 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Difficulty with creating table

I'm not sure what I'm doing wrong, but I've been wrestleing with this issue for the past few days (not all day, just when I've had time to play) and the resources I've been referencing have been less than useful

I'm trying to write my first Mod, and I'm trying to create a table in my database from a .php script. I'm using the following to try to insert a test table into my database and I'm getting errors with the function.

Code:
<?php

require_once('./global.php');

$DB_site->query(
	"CREATE TABLE " . TABLE_PREFIX . "addon_table1 (
	addon_table1_ID int() NOT NULL AUTO_INCREMENT PRIMARY KEY,
	addon_table1_field1 varchar() NOT NULL,
	addon_table1_field2 varchar() NOT NULL
	)");
?>

The error I'm getting is:

Fatal error: Call to a member function on a non-object in /home/username/public_html/forums/admincp/test_table.php on line 5


This is my first shot at playing with php and mySQL so I'm not sure what the problem with the query function is. As I understand it, including globab.php should have make the necessary links? Anyway, I'd appreciate any advice!

Scott
Reply With Quote
  #2  
Old 06-06-2008, 10:09 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The new database variable is $db there is an article in the Articles section about using the database class.
Reply With Quote
  #3  
Old 06-06-2008, 10:12 PM
MoT3rror MoT3rror is offline
 
Join Date: Mar 2007
Posts: 423
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

$DB_site was removed in vB 3.5 I believe.

You need to use $db or $vbulletin->db like this
PHP Code:
$vbulletin->db->query_write("
CREATE TABLE " 
TABLE_PREFIX "addon_table1 (
    addon_table1_ID int() NOT NULL AUTO_INCREMENT PRIMARY KEY,
    addon_table1_field1 varchar() NOT NULL,
    addon_table1_field2 varchar() NOT NULL
"
);

$db->query_write("
CREATE TABLE " 
TABLE_PREFIX "addon_table1 (
    addon_table1_ID int() NOT NULL AUTO_INCREMENT PRIMARY KEY,
    addon_table1_field1 varchar() NOT NULL,
    addon_table1_field2 varchar() NOT NULL
"
); 
You can read about the db class here.

Also I believe you need to define the length of the int and varchar when making the column like this.
[sql]
CREATE TABLE " . TABLE_PREFIX . "addon_table1 (
addon_table1_ID int(50) NOT NULL AUTO_INCREMENT PRIMARY KEY,
addon_table1_field1 varchar(100) NOT NULL,
addon_table1_field2 varchar(100) NOT NULL
)
[/sql]

Just a suggestion, if you have phpmyadmin, you can export the table structure and it will give the query to make the table.
Reply With Quote
  #4  
Old 06-06-2008, 11:35 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the quick reply!!! The out of data class might explain it. I was using some sample code which, now knowing what to look back at, is out of date.

The up to date class is working like a champ!!!

I'll look into the phpmyadmin, that sounds useful! Thanks for the tip!
Reply With Quote
  #5  
Old 06-07-2008, 06:35 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MoT3rror View Post
Also I believe you need to define the length of the int and varchar when making the column like this.
You don't have to define lengths. vBulletin doesn't do it when doing a fresh install (see mysql-schema.php).
Reply With Quote
Reply

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:46 PM.


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.03447 seconds
  • Memory Usage 2,193KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete