Alright, I have a random thing going on. Works perfectly fine. This is the code:


//To change the odds, change the second number in the rand() function.
$rand = floor(rand(0,1));
if($rand == 1)
	$sql = "SELECT * FROM randomevents WHERE rarity <= '10'";
	$result = mysqli_query($cxn, $sql);
	while ($row = mysqli_fetch_assoc($result))
		$event[] = $row['phrase'];
		if ($row['type'] == 'gainsp')
			$rand = rand(200,500);
			$sql = "UPDATE members SET starpoints = starpoints+$rand WHERE userid='".$_SESSION['userid']."'";
			mysqli_query($cxn, $sql) or die("Query died: updating starpoints");
	//This will pick a random event and show it
	$renum = floor(rand(0,count($event)));
	$eventdisplay = $event[$renum];

In the database, I have the phrases set as:
"You have gained {$rand} starpoints!".
How do you make that variable echo out as the $rand I'm generating on this page?
It just keeps posting as is. When I went the {$rand} to display as the number I'm generating to set their starpoints to.
So I guess how do you hold a variable in the database?

Does anyone understand what I'm asking?
Here as an image to help understand:


Member Avatar

Why not store it as a placeholder, like, %s.
USe sprintf or even str_replace to replace it with the variable of choice?

What do you mean a placeholder? Is that under the field type?..
I'm trying to do random events and they come out as a string.
How do I format that variable to.. output the string?
The value changes for every single user since it's a random number.

Member Avatar

You would have to replace { $rand } in your "phrase" with %s and then use sprintf like so :


$randpoints = rand(200, 500);
$phrase = "You have received %s starpoints !";
$userphrase = sprintf($phrase, $randpoints);