duanqian8867 2018-05-15 10:54
浏览 52
已采纳

使用php中的系统函数返回变量中的mysql错误

I am trying to import a database using system() in php. It works fine when my sql file is correct. lets say i have an error in the sql file(for eg. syntax error). I want that exact error in a variable so I can maintain a log and alert purpose.

<?php
    $lastline = system("mysql -u root mydbname < /home/mydbname.sql ",$retval);
    echo "
retval:";
    print_r($retval);
    echo "
lastline:";
    print_r($lastline);
?>

Unfortunately I don't get any errors in return when my sql is improper When I run the above code I get to see the errors on terminal but it does not save the errors in the variables.

  • 写回答

1条回答 默认 最新

  • duangenshi9836 2018-05-15 11:24
    关注
    <?php
    
    $filename = '/home/mydbname.sql';
    $mysql_host = 'localhost';
    $mysql_username = 'root';
    $mysql_password = '';
    $mysql_database = 'mydbname';
    
    // Connect to MySQL server & Select database
    mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
    mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
    
    // Temporary variable, used to store current query
    $templine = '';
    $lines = file($filename);
    foreach ($lines as $line)
    {
        // Skip it if it's a comment
        if (substr($line, 0, 2) == '--' || $line == '')
            continue;
    
        // Add this line to the current segment
        $templine .= $line;
    
        // If it has a semicolon at the end, it's the end of the query
        if (substr(trim($line), -1, 1) == ';')
        {
            // Perform the query
            mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
            // Reset temp variable to empty
            $templine = '';
         }
    }
     echo "Tables imported successfully";
    ?>
    

    Maybe importing this way would work, not sure this is what you need but it should work. Code from How to import .sql file in mysql database using php


    You could use the --force (-f) flag so that MySQL doesn't stop and just log any errors to the console:

    mysql -u userName -p -f -D dbName < script.sql
    <?php
        $lastline = system("mysql -u root -f mydbname < /home/mydbname.sql ",$retval);
        echo "
    retval:";
        print_r($retval);
        echo "
    lastline:";
        print_r($lastline);
    ?>
    

    PHP docs on exec() specify an additional 2 parameters for output and return value so maybe try getting both and one might include an error message.

    Hope this helps

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

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件