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";

}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.