If the question is precisely the substitution of a variable into a string, you can use the sprintf () function
And if you need it to form queries to the DBMS, then it is better to use PDO
Example from documentation:
$sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->execute(array(150, 'red')); $red = $sth->fetchAll();
And if you use mysqli and do not want to change it, then use mysqli :: prepare ()
Example from documentation:
$link = mysqli_connect("localhost", "my_user", "my_password", "world"); $city = "Amersfoort"; if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name = ?")) { /* связываем параметры с метками */ mysqli_stmt_bind_param($stmt, "s", $city); /* запускаем запрос */ mysqli_stmt_execute($stmt); /* связываем переменные с результатами запроса */ mysqli_stmt_bind_result($stmt, $district); /* получаем значения */ mysqli_stmt_fetch($stmt); printf("%s находится в округе %s\n", $city, $district); /* закрываем запрос */ mysqli_stmt_close($stmt); }