|
|
Question : check if table exists
|
|
I am using the DBI way of connecting to a mysql database, and want to check if a table exists
the code right now is: (sorry it's a lot)
#!/usr/bin/perl -w use CGI; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; print $q->header();
use DBI;
$add = $q->param("add");
$username = lc($q->param("username")); $password = $q->param("password"); $retypePassword = $q->param("retypepassword"); $name = $q->param("name"); $email = $q->param("email"); $health = "100"; $totalHealth = "100"; $weapon = "Dagger"; $weaponFP = "15"; $sWeapon = "LaserGun"; $sWeaponFP = "20"; $sPoints = "2"; $experience = "0"; $level = "1"; $gold = "10"; $avatar = $q->param("avatar");
if (!$add) { start(); }else{ add(); }
sub start() {
print "
";
} #End start();
sub add() {
# The connect is fairly specific to the DBMS # you are connecting to. This example is mysql. # # server machine (host)----------------+ # | # database----+ | # | | # V V my $db = DBI->connect('DBI:mysql:members:localhost', 'rpg', # user name 'admin', # password { RaiseError => 1});
if (!$username) { print "username blank \n"; $bad = "true"; } if (!$name) { print "name blank \n"; $bad = "true"; } if (!$email) { print "email blank \n"; $bad = "true"; } if (!$password) { print "password blank \n"; $bad = "true"; } if (!$retypePassword) { print "retype password please \n"; $bad = "true"; } if ($password ne $retypePassword) { print "passwords don't match \n"; $bad = "true"; } if (!$avatar) { print "avatar blank \n"; $bad = "true"; }
########################## ########################## ##########################i need this to work, check to see if it is already there, right now it does not work###########
my $sql = "select ID from $username"; my $con = $db->prepare($sql); if ($con->execute) { print "username: $username is taken \n"; $bad = "true"; }
########################## ########################## ##########################
if ($bad) { exit(); }
my $sql = "CREATE TABLE `$username` (`ID` SMALLINT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY, `password` TEXT NOT NULL, `name` TEXT NOT NULL, `email` TEXT NOT NULL, `health` INT(3) NOT NULL, `totalHealth` INT(3) NOT NULL, `weapon` TEXT NOT NULL, `weaponFP` INT(3) NOT NULL, `sWeapon` TEXT NOT NULL, `sWeaponFP` INT(3) NOT NULL, `sPoints` INT(2) NOT NULL, `experience` INT(5) NOT NULL, `level` INT(1) NOT NULL, `gold` INT(10) NOT NULL, `avatar` TEXT NOT NULL)"; my $con = $db->prepare($sql); $con->execute;
my $sql = "insert into $username values ('','$password','$name','$email','$health','$totalHealth','$weapon','$weaponFP','$sWeapon','$sWeaponFP','$sPoints','$experience','$level','$gold','$avatar')"; my $con = $db->prepare($sql); $con->execute;
$con->finish; $db->disconnect;
print " back > mysql.cgi</a> ";
} #End add();
|
Answer : check if table exists
|
|
let me write it my way, ok ? ;-) You can translate it if you want 8-)
$connect=mysql_connect('localhost','root','yourpassword') or die('could not connect'); mysql_select_db('testdb',$connect) or die('could not select DB'); $query="SELECT COUNT(*) FROM $username"; $result=mysql_query($query,$connect); // don't do this : or die("bad query '$query' : ".mysql_error()); if ($result) { // query executed ok : table exists echo "table $username does indeed exist. "; } else { // error this invalid query thus non-existent table ;-) echo "table $username does NOT exist. "; } // if result ?>
|
|
|
|
|