Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am creating a basic application framework just for a challenging project. Things are going good except for this silly problem:

function make_mysqli_connection_file($db_host,$db_user,$db_pass,$db_name){
	$mysqli_connect_file = fopen("includes/mysqli_connect.inc.php", "w") or die("Unable to open file!");
	$conn_string = '<?php $dbc = mysqli_connect ("' . $db_host . '", "' . $db_user . '", "' . $db_pass . '", "' . $db_name . '") OR die("Could not connect to MySql: " . mysqli_connect_error() );';
	fwrite($mysqli_connect_file, $conn_string);
	fclose($mysqli_connect_file);
}

This function will create an "includes" directory and a "mysqli_connect.inc.php" file with the standard connection string, it works as expected. The problem is that I want a newline after the opening php tag. I have tried double quoting the string, single quoting and all the necessary concatenation combinations necessary to keep the variables correct. The current output of the function is this:

<?php $dbc = mysqli_connect ("localhost", "root", "", "database_test") OR die("Could not connect to MySql: " . mysqli_connect_error() );

And what I want is this:

<?php 
$dbc = mysqli_connect ("localhost", "root", "", "database_test") OR die("Could not connect to MySql: " . mysqli_connect_error() );

It seems so simple, but the solution is eluding me. If I can get this solved I can move forward on other parts of this script.

 

Thanks for any advice!

Link to comment
Share on other sites

Thanks HartleySan for the reply. I got it to work with a variation of your suggestion. Before I posted for help, I did try exactly what you suggested, I got a newline, but I would get a "undefined variable $dbc" notice and $dbc would be blank in the generated string.

 

I had to initialize $dbc to a string literal '$dbc' for it to show up as a variable in the generated string.

 

Like this:

function make_mysqli_connection_file($db_host,$db_user,$db_pass,$db_name){
    $dbc='$dbc'; 
    $mysqli_connect_file = fopen("includes/mysqli_connect.inc.php", "w") or die("Unable to open file!");
    $conn_string = "<?php \r\n $dbc = mysqli_connect ('" . $db_host . "', '" . $db_user . "', '" .$db_pass . "', '" . $db_name . "') OR die('Could not connect to MySql: ' . mysqli_connect_error() )";
    fwrite($mysqli_connect_file, $conn_string);
    fclose($mysqli_connect_file);
}

Now I get the output I need in my auto generated connection script. This is a fun little project that I have become obsessed with...too much time on my hands I guess. Oh well, even if it goes nowhere, I learned a lot.

 

Thanks again.  

Link to comment
Share on other sites

Yes, if you use double quotes for the entire string, then you must do that, yes.

Personally, I prefer to use single quotes for everything but the newline characters, and then switch to double quotes for just those.

 

Either way though, you can solve your problem.

Link to comment
Share on other sites

 Share

×
×
  • Create New...