2011-07-07 60 views
1

我想將print_r輸出插入到數據庫中?插入print_r輸出到數據庫?

在數據庫中,結果爲1

如何解決這個問題?

<?php 
    processLog(print_r($dataArray)); 

    function processLog($text) { 
     global $process, $db, $groupID; 

     print($text . "\n"); 

     $SQL = "INSERT INTO enable_log (process_id, process_date, group_id, log_output, log_time) 
       VALUES(:process_id, :process_date, :group_id, :log_output, now())"; 

     $q = $db->prepare($SQL); 
     $q->bindValue(":process_id", $process['pid']); 
     $q->bindValue(":process_date", $process['date']); 
     $q->bindValue(":group_id", $groupID); 
     $q->bindValue(":log_output", $text); 
     $q->execute(); 
    } 
?> 
+1

閱讀php.net文檔。 – Gedrox

回答

4

代替存儲的print_r()輸出(你可以用@xzyfer的回答做),我會建議使用serialize()使PHP可以扭轉字符串返回到正確的數組,如果你需要將其從數據庫中拉出來。

processLog(serialize($dataArray)); 
+0

這是個好主意,謝謝。並非所有的processLog都是一個數組。有些可能只是文本。 –

4

傳遞true作爲第二個參數的print_r:

$a = print_r($var, true); 

print_r($var)輸出到$一個;

在您的例子:

$q->bindValue(":log_output", print_r($text, true)); 
2

如果您傳遞返回參數去的print_r您可以使用結果,而不是將它打印出來

$result = print_r($text, true);

$結果現在將包含什麼將先前已輸出。

請參閱說明書here