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

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

报告相同问题?

悬赏问题

  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真