Maniacility Posted July 28, 2009 Share Posted July 28, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/ Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 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. What do you want ? The seams seam to check if the names are taken! Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885362 Share on other sites More sharing options...
Maniacility Posted July 28, 2009 Author Share Posted July 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885368 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 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') "); ?> Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885381 Share on other sites More sharing options...
Maniacility Posted July 28, 2009 Author Share Posted July 28, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885401 Share on other sites More sharing options...
MadTechie Posted July 28, 2009 Share Posted July 28, 2009 it shouldn't duplicate anything! you sure the names are exact ? Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885404 Share on other sites More sharing options...
Maniacility Posted July 28, 2009 Author Share Posted July 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885425 Share on other sites More sharing options...
TeNDoLLA Posted July 28, 2009 Share Posted July 28, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885439 Share on other sites More sharing options...
Maniacility Posted July 29, 2009 Author Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885450 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885457 Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885463 Share on other sites More sharing options...
Maniacility Posted July 29, 2009 Author Share Posted July 29, 2009 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])); Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885478 Share on other sites More sharing options...
Maniacility Posted July 29, 2009 Author Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885485 Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885502 Share on other sites More sharing options...
Maniacility Posted July 29, 2009 Author Share Posted July 29, 2009 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!! Quote Link to comment https://forums.phpfreaks.com/topic/167858-solved-random-name-generator/#findComment-885516 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.