Jump to content

Recommended Posts

Hi, i'm trying to make a random name generator that generates a random name by getting the first name and last name from a text file, then comparing it to the current random names in the database. If the random name already exists, i want the script to find another random name, if it doesn't, i want it to insert it into the database.

 

As a test script, I did this and it didn't work.

 

//First Name

$quotes = "names.txt";

$quotes = file($quotes);

$ranNum = rand(0, count($quotes)-1);

 

$first_name = ($quotes[$ranNum]);

 

//End

// Last Name

$quotes2 = "names.txt";

$quotes2 = file($quotes2);

$ranNum2 = rand(0, count($quotes2)-1);

 

$last_name = ($quotes2[$ranNum2]);

///End

 

$fullname = "$first_name $last_name";

 

if (mysql_result(mysql_query("SELECT count(*) FROM `drug_dealers` WHERE `first_name` = '".$first_name."' AND `last_name` = '".$last_name."'"), 0) == 0)

{

echo "$fullname is inserted";

}

 

I have two people in my database, Kurtis Fehr and Matthew Blake.

The names in my names.txt file are Kurtis, Fehr, Matthew and Blake.

 

However with this current script I am getting the return of "Kurtis Fehr is inserted" and "Matthew Blake is inserted" but I don't want that.

 

Can anybody help me to fix this script or advise me to another?

 

Thanks! :D

Link to comment
https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/
Share on other sites

ye, what i want it to do is check if name already exists in the database, if it does i want it to regenerate a new random name, however if it doesn't i want it to insert it into the database.

 

That current line is supposed to only echo "Full Name is inserted" if the name doesn't exist... but Kurtis Fehr and Matthew Blake both exist in my database, so why is it echoing their names?

try this (untested)

<?php
$quotes = "names.txt";
$quotes = file($quotes);
do
{
$rand_names = array_rand($quotes, 2);
$first_name = $quotes[$rand_names[0]];
$last_name = $quotes[$rand_names[1]];
$result = mysql_query("SELECT count(*) FROM `drug_dealers` WHERE `first_name` = '$first_name' AND `last_name` = '$last_name' LIMIT 1");
$x = mysql_fetch_array($result);
}while($x[0] > 0);

echo "$first_name $last_name";
mysql_query("INSERT INFO `drug_dealers` SET (`first_name`, `last_name`) VALUES ('$first_name', '$last_name') ");
?>

try this (untested)

 

I updated it a little to work, i turnt it into this:

 

$quotes = "names.txt";
$quotes = file($quotes);
do
{
   $rand_names = array_rand($quotes, 2);
   $first_name = $quotes[$rand_names[0]];
   $last_name = $quotes[$rand_names[1]];
   $result = mysql_query("SELECT count(*) FROM `drug_dealers` WHERE `first_name` = '$first_name' AND `last_name` = '$last_name' LIMIT 1");
   $x = mysql_fetch_array($result);
}while($x[0] > 0);

echo "$first_name $last_name";
mysql_query("INSERT INTO `drug_dealers` (`first_name`, `last_name`) VALUES ('$first_name', '$last_name') ");

 

It inserts them fine, but it still inserts the same entry (so two people with the same name)

it shouldn't duplicate anything!

you sure the names are exact ?

 

yea, i'm sure... this is what my names.txt file looks like:

 

Kurtis
Fehr
Matthew
Blake

 

wait though, i just checked... it works!

however, it only works if every field in the database is the same e.g:

 

First Name: Kurtis

Last Name: Fehr

Born: London - England

 

if I try to insert one that was born in Manchester - England for example, it won't allow me to because the first and last name are the same?

You could probably create fullname inside do-while and change your SQL clause to something like this.. if there is result for fullname found in db do not add and if there is not add new name (first and last) to db.

 

$fullname = $firstName . ' ' . $fullName;
$r = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM some_table HAVING fullname = '$fullname'");

 

(not tested)

You could probably create fullname inside do-while and change your SQL clause to something like this.. if there is result for fullname found in db do not add and if there is not add new name (first and last) to db.

 

$fullname = $firstName . ' ' . $fullName;
$r = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM some_table HAVING fullname = '$fullname'");

 

(not tested)

 

nah no luck.

 

   $rand_names = array_rand($quotes, 2);
   $first_name = $quotes[$rand_names[0]];
   $last_name = $quotes[$rand_names[1]];
   $fullname = $first_name . ' ' . $last_name;
   $result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` HAVING fullname = '$fullname'");
   $x = mysql_fetch_array($result);

 

i used this.

You could probably create fullname inside do-while and change your SQL clause to something like this.. if there is result for fullname found in db do not add and if there is not add new name (first and last) to db.

 

$fullname = $firstName . ' ' . $fullName;
$r = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM some_table HAVING fullname = '$fullname'");

 

(not tested)

 

nah no luck.

 

   $rand_names = array_rand($quotes, 2);
   $first_name = $quotes[$rand_names[0]];
   $last_name = $quotes[$rand_names[1]];
   $fullname = $first_name . ' ' . $last_name;
   $result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` HAVING fullname = '$fullname'");
   $x = mysql_fetch_array($result);

 

i used this.

 

In addition to this you might have to change the comparison inside while.. maybe while(!is_null($x[0])) or while(strlen($x[0]) > 0) works? Probably both will do it.

In addition to this you might have to change the comparison inside while.. maybe while(!is_null($x[0])) or while(strlen($x[0]) > 0) works? Probably both will do it.

 

no luck, used this:

do
{
   $rand_names = array_rand($quotes, 2);
   $first_name = $quotes[$rand_names[0]];
   $last_name = $quotes[$rand_names[1]];
   $fullname = $first_name . ' ' . $last_name;
   $result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` HAVING fullname = '$fullname'");
   $x = mysql_fetch_array($result);
}while(!is_null($x[0]));

I'm confused, do you want to allow duplicate names or not ?

this class a duplicate as having the same first AND last name only

 

i'll explain myself a little more and i'll use the reason i need this script. :)

 

on my MMORPG, each of my users have "drug dealers" that sell drugs for them.

 

now, each of these drug dealers has a name, but i don't ever want a user to have a drug dealer with the same name.

 

e.g. two drug dealers both called Matthew Blake.

 

 

so, the reason i need it possible to insert the same person, but with different details, is so that the same person can be entered however for a different user.

 

so if the drug dealer Matthew Blake belongs to user1, then the database inserts a new drug dealer called Matthew Blake however for user2, that is ok.

 

I just can't have the same drug dealer for the same user.

 

I hope this all makes sense now. :)

Then you could just extend this SQL clause a bit and add a username in it or user_id which ever you have there to idenfity which name belongs to which user.

 

$result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` WHERE user_id = $userId HAVING fullname = '$fullname'");

 

or

 

$result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` WHERE user_name = '$useName' HAVING fullname = '$fullname'");

 

Note that in SQL clauses you have to use ' signs around string values but not around integers. Of course you have to be then sure you are placing integer as userId. Which you can make sure with php's intval() -function before using the integer (or id) in the clause.

Then you could just extend this SQL clause a bit and add a username in it or user_id which ever you have there to idenfity which name belongs to which user.

 

$result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` WHERE user_id = $userId HAVING fullname = '$fullname'");

 

or

 

$result = mysql_query("SELECT CONCAT(first_name, ' ', last_name) AS fullname FROM `drug_dealers` WHERE user_name = '$useName' HAVING fullname = '$fullname'");

 

Note that in SQL clauses you have to use ' signs around string values but not around integers. Of course you have to be then sure you are placing integer as userId. Which you can make sure with php's intval() -function before using the integer (or id) in the clause.

 

oh wow, it worked like a charm!

thank you so much!!

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.