doutang3760 2018-05-15 19:16
浏览 58
已采纳

将var_export保存到MySql数据库

I have a php script which executes a command on the server for example:

exec('sudo unoconv -f pdf '.$file, $output, $return);

Then I check if the script executed successfully:

if(!$return) { Do something here } else{ $errormsg = var_export($output, true); }

The error message might look something similar to this:

array ( 0 => 'Error: /invalidfileaccess in pdf_process_Encrypt', 1 => 'Operand stack:', 2 => '', 3 => 'Execution stack:', 4 => ' %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1910 1 3 %oparray_pop 1909 1 3 %oparray_pop 1893 1 3 %oparray_pop --nostringval-- --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push', 5 => 'Dictionary stack:', 6 => ' --dict:1162/1684(ro)(G)-- --dict:1/20(G)-- --dict:82/200(L)-- --dict:82/200(L)-- --dict:109/127(ro)(G)-- --dict:291/300(ro)(G)-- --dict:20/31(L)--', 7 => 'Current allocation mode is local', )

I then attempt to store the error message into my MySql database:

mysqli_query($con, "INSERT INTO incidents (operation, date, error) VALUES ('convert', '$date', '$errormsg')");

The column error is of data type VARCHAR(), when I execute the above query nothing is inserted into the database, if I remove $errormsg from the above command the data is successfully inserted into the database. Which data type should I use for the column so that the error message would be inserted?

  • 写回答

1条回答 默认 最新

  • duandingcu7010 2018-05-15 19:38
    关注

    The method seems to return a string. This code

    <?php
    
    $var = ['test' => 'ja'];
    $result = var_export($var, true);
    echo gettype($result);
    

    prints "string" to output. So as I understood, this method returns valid PHP-Code as a string.

    I would guess, that the problem is not related to this function but to the SQL-Query or code for storing the information.

    Update: I saw, that you posted an update. The problem is, that you are neither escaping the string nor using prepared statements. I would recommend to you, that you use prepared statements instead of string interpolation. This is also relevant with regards to security problems.

    See mysqli_real_escape_string or even better mysqli_prepare

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配