print variable with quotes in another php file

0

Friends, I have a problem with the "... really a headache, I have a php that writes another php code, something like this: Php1 writes content in a file called file and file must execute a mysql statement with the value of a variable that was in php 1

<?php
$subs_nombre = "Junio";
$miArchivo = fopen("archivo.php", "w");
$php='<?php 
consulta1 = "select cp_id from config_camp where cp_nombre = "'.$subs_nombre.'" ";
$consulta1 = mysql_query($consulta1, $link);
?>';                                                                                                                                              


fwrite($miArchivo, $php);                                                                                                                         
fclose($miArchivo); 
?>

The problem is that PHP 2 is written like this

<?php
consulta1 = "select cp_id from config_camp where cp_nombre = "Junio" ";
$consulta1 = mysql_query($consulta1, $link);
?>

And being this way when executing "file.php", php tells me that it is not possible since it has "Then I should go with single quotes", but I can not make June remain as 'June'.

Can someone give me a hand?

    
asked by Lusag silva 01.06.2018 в 21:48
source

3 answers

1

What you need to do is escape the single quotes between which will be the value of the variable $subs_nombre , to escape these quotes, just put an inverted slash \ before each, like this:

<?php
$subs_nombre = "Junio";
$miArchivo = fopen("archivo2.php", "w");
$php='<?php 
consulta1 = "select cp_id from config_camp where cp_nombre = \''.$subs_nombre.'\'";
$consulta1 = mysql_query($consulta1, $link);
?>';                                                                                                                                              


fwrite($miArchivo, $php);                                                                                                                         
fclose($miArchivo); 
?>

Achieving a valid exit

<?php 
consulta1 = "select cp_id from config_camp where cp_nombre = 'Junio'";
$consulta1 = mysql_query($consulta1, $link);
?>
    
answered by 01.06.2018 / 21:55
source
0

Always when using double quotes and in need of quotes again the simple ones should be used, also the opposite applies in most languages,

simple example

echo "<div class='row'>";

or otherwise

echo '<div class="row">';
    
answered by 01.06.2018 в 22:33
0

Try this.

$ArcivoPhp = <<<Query
   <?php
      echo "hola mundo";
   ?>
Query;

echo $ArchivoPhp;
    
answered by 01.06.2018 в 23:16