arbitter Posted August 20, 2010 Share Posted August 20, 2010 Hi there! On a page of mine, random tables get generated. The tables consist of 8 characters, so there's 5 . 10^13 possibilities. Yet ofcourse there is a possibility that 2 tables with the same name can be generated, which ofcourse is not desired. So I need an adjustment that can see whether there is already a name that's the same, and if so, make another random name. And if that one also exists, make a new one again. Untill the name doesn't exist yet and it remains. Here's the script I have right now: function createName($length) { $chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $i = 0; $password = ""; while ($i <= $length) { $password .= $chars{mt_rand(0,strlen($chars))}; $i++; } return $password; } $name = createName(; mysql_select_db($database, $con); $sql = "CREATE TABLE " . $name . "( id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), devraag varchar(500), weergave varchar(500), naam varchar(500), inhoud varchar(500), tijd varchar(100))"; mysql_query($sql, $connection)or die(mysql_error()); I'm not goot with the combination of PHP and mysql, and really have no Idea how to do this. Any help? Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/ Share on other sites More sharing options...
MatthewJ Posted August 20, 2010 Share Posted August 20, 2010 mysql_select_db('database', $conn); $res = mysql_query("SHOW TABLES"); $row = mysql_fetch_assoc($res); $tables = array(); do { $tables[] = $row['Tables_in_database']; } while ($row = mysql_fetch_assoc($res)); if(!in_array($table_name, $tables)) { //Name is available } else { // Name exists } Something like that should work Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101664 Share on other sites More sharing options...
kickstart Posted August 20, 2010 Share Posted August 20, 2010 Hi Above should do it, but why do you want to generate tables like that? Seems to be defeating the object of a relational database. All the best Keith Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101668 Share on other sites More sharing options...
arbitter Posted August 20, 2010 Author Share Posted August 20, 2010 But in the else-part; shouldn't it automaticaly make a new name then? I've never used a do-while loop, and arrays isn't my cup of tea. Keith; I made a page for my mother in which she can create a new 'page' with a question and people can then react to that on that page. That's why I have a table generated, one every page, where the comments can be placed. True, it could be done in one table with an extra column... But it seemed easier like this. Believe me, she isn't going to create more than 10 pages, so 10 tables, but ofcourse theres always a small tiny chance of 2 being with the same name. Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101671 Share on other sites More sharing options...
kickstart Posted August 20, 2010 Share Posted August 20, 2010 Hi Fair enough, but even then I have visions of her hitting the button to reload a page when the form is submitted to create a page and so creating dozens of copies of the same page each with their own table. All the best Keith Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101676 Share on other sites More sharing options...
arbitter Posted August 20, 2010 Author Share Posted August 20, 2010 Keith My mother is keen with computers and the internet, she is a programmer herself. I've explained how the site works so I don't believe she'll accidentally make any copies. Our internet and server is fast enough to not give you the possibility to click the button more then once, and also she gets redirected after generating a page. And if she does make some of them accidentally; she'll be able to delete them herself! Or at least that's something I'm trying to make possible with ajax but it isn't really going that well... So much work only to get one popup in the middle of the deleting process! Kind regards Joren Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101682 Share on other sites More sharing options...
kickstart Posted August 20, 2010 Share Posted August 20, 2010 she'll be able to delete them herself! Harsh but fair . Ajax can be fun. One system I work on has hundreds of databases each for different clients. I landed up using Ajax to display data from multiple tables, with each client in its own DIV on screen and then using Ajax to populate the contents of the div. Takes far too long otherwise to connect to the separate databases All the Keith Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101684 Share on other sites More sharing options...
arbitter Posted August 20, 2010 Author Share Posted August 20, 2010 And if she fails to push a delete button; I'll do the work for her! Still having trouble with the code though; what must I put in the else? Because it should loop shouldn't it... Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101706 Share on other sites More sharing options...
MatthewJ Posted August 20, 2010 Share Posted August 20, 2010 I just put the comments in for you to replace them with what you need to do... I'm confused as to what you mean by "they should loop". in_array() will check the entire array for the table name... if it is there, you want to generate a new name, if it is not, you can continue on with your table creation. so just generate your random name... then run the code I posted and do the appropriate actions when needed. Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101709 Share on other sites More sharing options...
arbitter Posted August 20, 2010 Author Share Posted August 20, 2010 Perhaps I'm just misinterpretating it. But with this code: if(!in_array($table_name, $tables)) { //Name is available } else { // Name exists } If the name is available, I'll generate the table and all that stuff. But why is there an option that the code is not unique? In the end, I only want 1 code, and that code should not exist yet.. Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101711 Share on other sites More sharing options...
kickstart Posted August 20, 2010 Share Posted August 20, 2010 Hi Putting the code together [php <?php function createName($length) { $chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $i = 0; $password = ""; while ($i <= $length) { $password .= $chars{mt_rand(0,strlen($chars))}; $i++; } return $password; } mysql_select_db('database', $conn); $res = mysql_query("SHOW TABLES"); $row = mysql_fetch_assoc($res); $tables = array(); while ($row = mysql_fetch_assoc($res)) { $tables[] = $row['Tables_in_database']; } while(in_array($table_name, $tables)) { $table_name = createName(; } mysql_select_db($database, $con); $sql = "CREATE TABLE " . $table_name . "( id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), devraag varchar(500), weergave varchar(500), naam varchar(500), inhoud varchar(500), tijd varchar(100))"; mysql_query($sql, $connection)or die(mysql_error()); ?> [/code] All the best Keith Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101712 Share on other sites More sharing options...
arbitter Posted August 20, 2010 Author Share Posted August 20, 2010 Keith, I have nothing else to say except that you're an angel! My gratitude also goes to MatthewJ ofcourse! My intellect isn't that brilliant, that's why I couldn't interpret it correctly, my bad! I'm not quite sure if works, but it creates the databases so I guess it'll check it too, thanks guys! Link to comment https://forums.phpfreaks.com/topic/211289-use-a-loop-and-compare-with-database-for-unique-name/#findComment-1101718 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.