vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Difficulty with creating table (https://vborg.vbsupport.ru/showthread.php?t=181733)

AsscBB 06-06-2008 08:55 PM

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

Opserty 06-06-2008 09:09 PM

The new database variable is $db there is an article in the Articles section about using the database class.

MoT3rror 06-06-2008 09:12 PM

$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.

AsscBB 06-06-2008 10:35 PM

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!

Dismounted 06-07-2008 05:35 AM

Quote:

Originally Posted by MoT3rror (Post 1542657)
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).


All times are GMT. The time now is 04:33 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.01973 seconds
  • Memory Usage 1,724KB
  • 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_code_printable
  • (1)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)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