penislandbic Posted January 4, 2014 Share Posted January 4, 2014 (edited) I have a php file that has this in it... //Make logChatInput.php $dataBase = "users"; $logChatInput = fopen($newDir."/logChatInput.php", "w"); $logChatInput_contents = <<<EOD <?php $chatInput = addslashes($_GET["chatInput"]); date_default_timezone_set('America/Chicago'); $time = date('g:i A'); $crappychat_service = mysql_connect("localhost", "root", ""); mysql_select_db("{$dataBase}", $crappychat_service); $sql = "INSERT INTO messages (Message) VALUES('".$chatInput."')"; mysql_query($sql, $crappychat_service); ?> EOD; fwrite($logChatInput, $logChatInput_contents); fclose($logChatInput); My problem is I want ONLY $dataBase to be recognized as a the value it holds and not anything else. The php is giving an error because I don't have $chatInput declared outside the EOD. I only want the $dataBase variable to be associated with the current PHP file and not the one being written, so I put it in curly braces, but it still recognizes other "variables". How would have it only recognize $dataBase in the EOD? Edited January 4, 2014 by penislandbic Quote Link to comment https://forums.phpfreaks.com/topic/285094-heredoc-variable/ Share on other sites More sharing options...
JIXO Posted January 4, 2014 Share Posted January 4, 2014 Here is the HEREDOC documentation, I'm not sure of what u r trying to achive, for example : $crappychat_service = mysql_connect("localhost", "root", "123"); $crappychat_service will give you something like Resource #14, not usefull result to write in a file. Try using this : <?php //Make logChatInput.php $dataBase = "users"; $logChatInput = fopen($newDir."/logChatInput.php", "w"); date_default_timezone_set('America/Chicago'); $time = date('g:i A'); $chatInput = addslashes($_GET["chatInput"]); $connectionStatus = NULL; $crappychat_service = mysql_connect("localhost", "root", "123"); $connectionStatus = ($crappychat_service) ? 'Connected' : 'Failed to connect'; $dbSelectionStatus = NULL; $dbSelectionStatus = (mysql_select_db("{$dataBase}", $crappychat_service)) ? 'DB selected' : 'DB not found'; $sql = "INSERT INTO messages (Message) VALUES('".$chatInput."')"; $queryStatus = NULL; $queryStatus = (mysql_query($sql, $crappychat_service)) ? 'Query OK' : 'Query failed'; $logChatInput_contents = <<<EOD chatInput value : $chatInput MySql connection : $connectionStatus Database selection : $dbSelectionStatus SQL : $sql Query status : $queryStatus EOD; var_dump($logChatInput_contents); fwrite($logChatInput, $logChatInput_contents); fclose($logChatInput); Will output : string ' chatInput value : dslfkd MySql connection : Connected Database selection : DB not found SQL : INSERT INTO messages (Message) VALUES('dslfkd') Query status : Query failed' (length=209) I hope this sets u in the right direction Quote Link to comment https://forums.phpfreaks.com/topic/285094-heredoc-variable/#findComment-1463875 Share on other sites More sharing options...
Solution kicken Posted January 4, 2014 Solution Share Posted January 4, 2014 If you want a variable name treated as a literal string rather than as a variable name, then escape the $. Eg: $var='foo'; echo "The value of \$var is $var\n"; would output The value of $var is foo Quote Link to comment https://forums.phpfreaks.com/topic/285094-heredoc-variable/#findComment-1463886 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.