Jump to content

Heredoc variable


penislandbic

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/285094-heredoc-variable/
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/285094-heredoc-variable/#findComment-1463875
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.