简体   繁体   中英

How to include a variable in results from SQL PHP?

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM