PDA

View Full Version : Difficulty with creating table


AsscBB
06-06-2008, 09:55 PM
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.

<?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, 10: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, 10:12 PM
$DB_site was removed in vB 3.5 I believe.

You need to use $db or $vbulletin->db like this

$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 (https://vborg.vbsupport.ru/showthread.php?t=119350&highlight=database).

Also I believe you need to define the length of the int and varchar when making the column like this.

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
)


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, 11: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, 06:35 AM
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).