Jump to content

[SOLVED] Can someone please tell me what is wrong with this?


maltech

Recommended Posts

Everytime I load the page it tells me "created it" as in it never actually creates it, or it is not checking if it exists properly.

 

What I need it to do is see if the user (which is allready defined with $user_id earlier in the page) has a table created for them already. If they do not, create a table with their user id.

 

mysql_connect("localhost", "$dbuser", "$dbpass");

mysql_select_db("$dbname") or die( "Unable to select database");

function table_exists($user_id, $dbname) {

$exists = mysql_query("SELECT 1 FROM `$user_id` LIMIT 0");

if ($exists)

return true;

else

return false;

}

if (table_exists($user_id, $dbname)) {

printf("Table found");

} else {

mysql_query("CREATE TABLE $user_id()");

echo "created it";

}

You are echoing "created it" without checking that the query was successful.

 

The query is actually failing because you must create at least one column when you create the table. The mysql_error() you get when you check it is - "A table must have at least 1 column"

 

You can actually just use the following syntax, no need to test if it already exists -

 

CREATE TABLE IF NOT EXISTS tbl_name ...

 

Lastly, you should not be making separate tables for each user_id, doing so will make your code impossibly complicated and is a bad design.

 

So in your example the following should work fine then

 

mysql_connect("localhost", "$dbuser", "$dbpass");

mysql_select_db("$dbname") or die( "Unable to select database");

mysql_query("CREATE TABLE IF NOT EXISTS $user_id(

name varchar(100))");

mysql_close();

 

is that the correct format?

 

Also I do not understand why having a seperate table for each user_id would be so bad, the $user_id is different for each person, but will always be the same for an individual when they connect in the situation I am using it. My attempt is to use the table as a variables list for each user. I need to call the fields with more than just php and in all clients, php flash etc etc the $user_id will be the same because it comes from one source.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.