0

I want to generate connector.php file during installation like this

$file = "connector.php";
$fp = fopen($file,"wb");

$content = "<?php $con=mysqli_connect('";
$content += $host;
$content += "', '";
$content += $user;
$content += "', '";
$content += $password;
$content += "', 'blog');  if (mysqli_connect_errno()) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); }?>";

fwrite($fp,$content);
fclose($fp); 

but when I try this code, I just generate empty file (when I try some random text, it works fine), so I think, the problem is, that PHP trying to interpret everything between <?php and ?>. How can I make this code work, and make PHP not interpret code between php tags.

HamZa
  • 13,530
  • 11
  • 51
  • 70
JohnDow
  • 1,060
  • 4
  • 18
  • 40
  • ...and just why are you dynamically generating a php file for a database when you could just have a config file and `require_once()` it? – Amelia Aug 11 '13 at 19:59
  • When I try to generate file only with ' – JohnDow Aug 11 '13 at 19:59
  • @Hiroto I am trying to create automated installation – JohnDow Aug 11 '13 at 20:00
  • ...no, really, **why** are you generating a file in the first place? This is not how you automate installation of things in PHP. Generate a config file if needed, but don't dynamically generate code unless you know exactly what you are doing. – Amelia Aug 11 '13 at 20:02
  • 1
    Hint: this is not JavaScript ! – HamZa Aug 11 '13 at 20:02
  • Thank you guys, now it works, I will be able to accept best answer in 8 minutes! – JohnDow Aug 11 '13 at 20:03

2 Answers2

4

Use .= as concatenation operator.

Using + probably converts the whole thing to an integer. You also have to use single quotes around the first line in the second code block because PHP tries to interpret the undefined variable $con otherwise.

$file = "connector.php";
$fp = fopen($file,"wb");

// Use single quotes here, otherwise PHP tries to interpret $con
$content = '<?php $con=mysqli_connect(\'';
$content .= $host;
$content .= "', '";
$content .= $user;
$content .= "', '";
$content .= $password;
$content .= "', 'blog');  if (mysqli_connect_errno()) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); }?>";

fwrite($fp,$content);
fclose($fp); 
ComFreek
  • 27,416
  • 16
  • 97
  • 150
4

+= is a numeric operator and .= is a string operator. You're currently using += in your code and it will cause PHP to interpret it as a number.

The + or += operator first converts the values to integers (and all strings evaluate to zero when cast to ints) and then adds them, so you get 0. That's why your code isn't producing the expected result.

See ComFreek's answer above.

Also, an unrelated issue: to tell PHP not to use the actual variable values in the string, simply escape them, like so:

$content = "<?php \$con=mysqli_connect('";

Or, you can use single quotes:

$content = '<?php $con=mysqli_connect(\'';

reference: #2202331

Community
  • 1
  • 1
Amal Murali
  • 70,371
  • 17
  • 120
  • 139