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

<?php
include("config.php");

//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:

[img]http://i370.photobucket.com/albums/oo149/TenaciousMug/databasevariables.png[/img]

Dani AI

Generated

Short answer: the database string is plain text — MySQL will not expand PHP variables inside it. Store a template with a token (for example {points}), generate the numeric value in PHP, update the user row, then replace the token in the template before output.

A safe, clear workflow (avoid reusing a $rand flag variable and fix the array-index bug in the original post):

  • Pick or fetch one random event (SQL ORDER BY RAND() LIMIT 1 for small tables, or use array_rand() on a fetched array).
  • Generate the points value (e.g. $points = rand(200,500)).
  • Update the member row with a prepared statement so the increment is atomic.
  • Substitute a named token in the phrase (e.g. replace {points}) and escape the output for HTML.

Example (illustrative — not the same code already posted in the thread):

$points = rand(200,500);

/* safe prepared update */
$stmt = $cxn->prepare("UPDATE members SET starpoints = starpoints + ? WHERE userid = ?");
$stmt->bind_param("ii", $points, $_SESSION['userid']);
$stmt->execute();

/* replace named token and escape output */
$display = strtr($dbPhrase, ['{points}' => $points]);
echo htmlspecialchars($display, ENT_QUOTES, 'UTF-8');

Notes and troubleshooting:

  • The original floor(rand(0,count($event))) can generate an out-of-range index — use array_rand() or rand(0, count($event)-1).
  • Don’t reuse $rand for different purposes; use descriptive names like $shouldTrigger and $points.
  • Avoid eval() — it’s unsafe and unnecessary.
  • For large tables, ORDER BY RAND() is slow; consider selecting a random id range instead.
  • As and pointed out, templates/placeholders are the right idea — named tokens plus prepared updates and output escaping give a robust, secure solution for per-user variable text.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

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 for Member #900861

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

<?php

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

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.