Jump to content

Use a loop and compare with database for unique name


arbitter

Recommended Posts

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?

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

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.

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

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

she'll be able to delete them herself!

 

Harsh but fair ;D .

 

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

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.

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

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(8);
}

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

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!

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.