script help mysql tables

Hi,

Can anyone help me with a script, I have tried but just cannot get it to work.

Basically I want a script to check a mysql database to see if a table exists from a string. So I have a $username string on my site, so when the user goes to this page if a table all ready exists which is named the same as his username it displays one message, if it doesnt exist it creates the table. Also to display another message if $username is anonymous.

So for example:

$username = fred

if table fred exists then display “A table all ready exists”
if table fred does not exist display “Create table”
if $username = “anonymous” display “You must login”

Thanks in advance

Jay

Hi Jay,

So you want to create a new table out of $username value and you want it unique. A simple if/else will do. Take a look:

[php]
$username=‘some_data’;
if ($username==‘anonymous’){
echo ‘You must login!’;
}
else{
$sql=mysql_query(“CREATE TABLE IF NOT EXISTS ‘$username’ (columnname datatype)”, $connection) or die(“Unable to complete your request”.mysql_error());

if(!$sql){
echo “A table already exists”;
}
}
[/php]

I hope this answers your question. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service