Jump to content

Heredoc variable


Go to solution Solved by kicken,

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?

Edited by penislandbic
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.