Jump to content

[SOLVED] Need suggestion: Random MySQL row


brentech

Recommended Posts

 

	/* Get Names */
If (rand('1', '10') <  {
	$nat = $u_a["tloc"];
}
else {
 	$foreign = array(1 => 'Brazil', 'Canada', 'Japan', 'Mexico', 'Russia', 'Thailand', 'U.S.A.');
	$nat = $foreign[rand('1', '7')];
}

$fq = "SELECT name FROM firstnames WHERE nat = '$nat'";
$result = mysql_query($fq, $conn);

 

So the last 2 lines are what we're looking at here.

I'm doing a query which returns all the first names in my table that holds ID|NAME|NAT (nationality). The code above is deciding which nationality the name should be coming from...based on an % chance that generated person doesn't come from the same country as the team. (kinda meaningless to my real question, but background always helps)

 

My problem is with randomizing the selection of names. I can't seem come across a great way go about what I need done - and I feel like it should be simple.

So I'm wanting to select all the names which would be set to the particular nationality: $fq = "SELECT name FROM firstnames WHERE nat = '$nat'";

 

I couldn't find a MySQL function that could randomize from my select criteria, so I began thinking that it would be easier to do this by storing all the names of that nationality into an array and picking a random element of the array - which would be the name.....then, I ran into the problem of not being able to find a PHP function that would store more than a single row's values into an array.

 

I feel like I'm completely overlooking a function, or just WAY over-thinking the solution to my problem.

A friend at work suggested creating MySQL table views, but I'm not real familiar with those, or if that would be the best answer. Anyone have any tips or suggestions on functions/methods?

 

Thanks,

 

-Brent

Link to comment
Share on other sites

Was definitely making things way too complicated. Edited my query to randomize the order and limit the result to 1 row...so it just returns the one name as need. Thought I'd share my solution.

 

$fq = "SELECT name FROM firstnames WHERE nat = '$nat' ORDER BY RAND() LIMIT 1";
$result = mysql_query($fq, $conn);
$name = mysql_fetch_row($result);

$name[0] becomes the value I needed.

Link to comment
Share on other sites

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.