1

I have a MySQL database that contains cells with variable and I want to echo them in PHP with a current variable.

Example:

$source = "TEST",

$sql="SELECT * FROM data ORDER BY RAND()";
$result = mysqli_query ($connection,$sql);
if(mysqli_num_rows($result)!=0){
        $data = $row['data'];
}

echo $data;

echo $data display

www.domain.com?$source

but, as final results, I have to get

www.domain.com?TEST

How to do that?

*Info from MySQL can't be updated/edited

1 Answers1

0

If the variable name (substring) inside the $data string will always going to be '$source', then you can use the str_replace function.

Very important: Use single quotes ('), not double quotes (") to represent '$source' as a literal string. Do read: What is the difference between single-quoted and double-quoted strings in PHP?

Try the following:

$source = "TEST",

$sql="SELECT * FROM data ORDER BY RAND()";
$result = mysqli_query ($connection,$sql);
if(mysqli_num_rows($result)!=0){
        $data = $row['data'];
}

echo str_replace('$source', $source, $data);
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57