moosey_man1988 Posted October 6, 2015 Share Posted October 6, 2015 (edited) Hi Everyone im currently creating an install script and i was hoping to touch and cat to a lot of files for them to be created, but im having troubles. You will easily see in the code below what im trying to achieve. //Lets create the mysqli_connection page for all other php pages to use with the new DB user exec ('touch database/mysqli_connection.php'); exec ('echo "<?php $mysqli_connection = mysqli_connect('.$servername.','. $dbnewuser.','. $newuserpass.','. $dbname.'); if (!$mysqli_connection){ die("Connection Failed: " . mysqli_connect_error()); } ?>" > database/mysqli_connection.php'); the issue I have is this is how the file comes out, its having trouble passing $mysqli_connection as a string. see the mysqli_connection.php below <?php = mysqli_connect(localhost,testuser,testPassword,testDB); if (!){ die(Connection Failed: . mysqli_connect_error()); } ?> I did try using single quotes around the $mysqli_connection variable like so '.$mysqli_connection.' but the file then comes out blank. any help given would be greatly appreciated, and i thank everyone for their help in advance Edited October 6, 2015 by moosey_man1988 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 6, 2015 Share Posted October 6, 2015 Well, nesting three languages just isn't a good idea: You have a PHP code generating shell code generating PHP code. No wonder you have syntax conflicts. This can be made a lot simpler. PHP can in fact write files, there's no need to delegate this to the shell. There's also no need to generate an entire PHP script. Just define a bunch of constants and put the application logic into static scripts: <?php const DB_USER = <insert string here>; const DB_PASSWORD = <insert string here>; ... You should also consider using a simple data format like JSON, YAML or XML instead of a full-blown PHP script. Quote Link to comment Share on other sites More sharing options...
moosey_man1988 Posted October 6, 2015 Author Share Posted October 6, 2015 those are yet languages on my list to learn this script will ever be improved upon with new langues as I learn more languages etc etc. Thanks for the advice, im still not clear on ALL of a functions that PHP can do, but this is what forums and guides are for I will test out the put_file_contents, Thanks for a swift response Quote Link to comment Share on other sites More sharing options...
moosey_man1988 Posted October 6, 2015 Author Share Posted October 6, 2015 (edited) i created a test script, still no luck <?php $servername = "localhost"; $username = "root"; $DBpassword = "testPassword"; $dbname = "testDB"; $invoiceDB = "invoices"; $dbnewuser = "testUser"; $newuserpass = "testPass"; $file = "database/mysqli_connection.php"; $myString = "<?php $mysqli_connection = mysqli_connect($servername,$dbnewuser,$newuserpass,$dbname); if (!$mysqli_connection){ die('Connection Failed: ' . mysqli_connect_error()); } ?>"; file_put_contents($file, $myString); $getData = file_get_contents($file); echo $getData; ?> Ignore the unused variables, this was just something quick to see if can be accomplished. here's the error PHP Notice: Undefined variable: mysqli_connection in /var/www/html/test.php on line 14 PHP Notice: Undefined variable: mysqli_connection in /var/www/html/test.php on line 19 I really do hope to make a connection script, as once the installation finished it, all the rest of my script call upon the mysqli_connection.php Edited October 6, 2015 by moosey_man1988 Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted October 6, 2015 Solution Share Posted October 6, 2015 (edited) This makes no sense. You want $servername, $dbnewuser etc. to be replaced with the corresponding values, but at the same time you want $mysqli_connection to show up as a literal variable in the generated script. How is PHP supposed to tell the difference? If you want a literal dollar sign within a double-quoted string, you need to escape it with a backslash: echo "\$some_variable"; Secondly, you can't just insert strings into your code template, because those will end up as raw words. What you want are string literals, and those have quotes: $string_content = 'yada yada yada'; echo "String literal: '".addslashes($string_content)."'"; You should escape the content with addslashes() so that a literal quote within the string content won't blow up your generated PHP script. Edited October 6, 2015 by Jacques1 Quote Link to comment Share on other sites More sharing options...
moosey_man1988 Posted October 6, 2015 Author Share Posted October 6, 2015 Hey Jaques, Thank you for your response again your answer solved it for me VERY quickly, I will remember in future that magical backslash Resulting Code below //Lets create the mysqli_connection page for all other php pages to use with the new DB user $mysqli_file = "database/mysqli_connection.php"; $mysqli_connection_setup = "<?php \$mysqli_connection = mysqli_connect($servername, $dbnewuser, $newuserpass, $dbname); if (!\$mysqli_connection){ die('Connection Failed: ' . mysqli_connect_error()); } ?>"; file_put_contents($mysqli_file, $mysqli_connection_setup); Thanks again you must grow tired of PHP newbs Quote Link to comment Share on other sites More sharing options...
seandisanti Posted October 7, 2015 Share Posted October 7, 2015 (edited) Thanks again you must grow tired of PHP newbs At one point or another we were all there. Edited October 7, 2015 by seandisanti Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.