maltech Posted July 8, 2009 Share Posted July 8, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/165234-solved-can-someone-please-tell-me-what-is-wrong-with-this/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 8, 2009 Share Posted July 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165234-solved-can-someone-please-tell-me-what-is-wrong-with-this/#findComment-871337 Share on other sites More sharing options...
maltech Posted July 8, 2009 Author Share Posted July 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165234-solved-can-someone-please-tell-me-what-is-wrong-with-this/#findComment-871347 Share on other sites More sharing options...
Mchl Posted July 8, 2009 Share Posted July 8, 2009 Also I do not understand why having a seperate table for each user_id would be so bad Here's why http://www.phpfreaks.com/blog/giuseppe-maxia-on-database-normalisation-and-smoking Quote Link to comment https://forums.phpfreaks.com/topic/165234-solved-can-someone-please-tell-me-what-is-wrong-with-this/#findComment-871491 Share on other sites More sharing options...
maltech Posted July 8, 2009 Author Share Posted July 8, 2009 Thanks for that, that was a good read! Quote Link to comment https://forums.phpfreaks.com/topic/165234-solved-can-someone-please-tell-me-what-is-wrong-with-this/#findComment-871512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.