PDA

View Full Version : vbulletin SQL Help! - ISO1


NanoEntity
Sat 9th Jun '01, 10:52pm
Hello, I'm building an install script, my questions are:


1) How would I write a command that checks if a table exists and if it doesn't it creates it?

2) checking for Alter table/data id, if it dosent exist it creates it.

ALTER TABLE forum ADD test_table varchar(64)

DELETE FROM template WHERE title = 'test_table'

Just like a command for:
DROP TABLE IF EXISTS test_table

same for templets:
install_template("test_templet","data");

any ideas? here is a semple of my install script to show you what i have, please if someone knows how to create a check code addon to look if somethign exists that I want to insert, I be very happy!

<!-- Database error in vBulletin Control Panel: Invalid SQL: DELETE FROM test_table WHERE id = '7'
mysql error: Table 'dbtest.test_table' doesn't exist
mysql error number: 1146

<!-- Database error in vBulletin Control Panel: Invalid SQL: ALTER TABLE forum ADD test_table varchar(64);
mysql error: Duplicate column name 'test_table'

There seems to have been a slight problem with the database.
Please try again by pressing the refresh button in your browser.
An E-Mail has been dispatched to our Technical Staff, who you can also contact if the problem persists.
We apologise for any inconvenience.

GRRRR, I need that checking code! HELP!




<?php
error_reporting(7);

require ("./global.php");

$newversion = "2.0.x Release ISO1";
$thisscript = "install.php";

function gotonext($extra="") {
global $step,$thisscript,$DB_site,$query;
$nextstep = $step+1;
echo "<p><b><a href=\"$thisscript?step=$nextstep\">Continue With Install </a>—› » $extra</b></p>\n";
}

function install_template($title, $template) {
global $DB_site;
$DB_site->query("INSERT INTO template (templateid, templatesetid, title, template) VALUES ('', '-1', '$title', '$template')");
}


if (!$step) {
$step = 1;
}


// ******************* STEP 1 *******************
if ($step==1) {



gotonext();

}


// ******************* STEP 2 *******************
if ($step==2) {

$DB_site->query("DROP TABLE IF EXISTS test_table");
$DB_site->query("DELETE FROM test_table WHERE id = '7' ");
$DB_site->query("DELETE FROM setting WHERE settingid = '200'");
$DB_site->query("ALTER TABLE forum DROP test_table varchar(64) ");
$DB_site->query("DELETE FROM template WHERE title = 'forumhome_lastpostby'");


echo "<p>Done!\n";

gotonext();
}


// ******************* STEP 3 *******************
if ($step==3) {

$DB_site->query("CREATE TABLE test_table (
id int(11) NOT NULL auto_increment,
forumid int(11),
dpost int(11),
bpost int(11),
reqreply tinyint(4),
PRIMARY KEY (id)

)");

$DB_site->query("INSERT INTO test_table VALUES (' 7',' 2',' 150',' 150','1')");
$DB_site->query("ALTER TABLE forum ADD test_table varchar(64);");
$DB_site->query("INSERT INTO settinggroup VALUES (200,'test_table',200)");

}

?>


Anyhow, you get the point, I want a checking code addon that checks for the table/alter/templets exist or not for code inserts.

Thank you for your support!

Zarkov
Sun 10th Jun '01, 7:06am
heres the first problem

$DB_site->query("DROP TABLE IF EXISTS test_table");
$DB_site->query("DELETE FROM test_table WHERE id = '7' ");


The table has been deleted if it existed then the next thing you try to do is remove data from it.
Either dont drop the table or dont remove the data.
the second is more logical as if the table is gone then there is no data to delete anyways.