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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化