错误报告?从PHP导入.sql文件并显示错误。

时间:2021-11-14 22:01:46

I am building a way of importing .SQL files into a MySQL database from PHP. This is used for executing batches of queries. The issue I am having is error reporting.

我正在构建一种从PHP导入. sql文件到MySQL数据库的方法。这用于执行多批查询。我遇到的问题是错误报告。

$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command, $output);

This is essentially how I am importing my .sql file into my database. The issue is that I have no way of knowing if any errors occurred within the PHP script executing this command. Successful imports are entirely indistinguishable from a failure.

这本质上是我将.sql文件导入数据库的方式。问题是,我无法知道在执行此命令的PHP脚本中是否发生了任何错误。成功的进口与失败是完全不可区分的。

I have tried:

我有尝试:

  • Using PHP's sql error reporting functions.
  • 使用PHP的sql错误报告函数。
  • Adding the verbose argument to the command and examining the output. It simply returns the contents of the .sql file and that is all.
  • 向命令添加详细参数并检查输出。它只返回.sql文件的内容,仅此而已。
  • Setting errors to a user variable within the .sql file and querying it from the PHP script.
  • 将错误设置为.sql文件中的用户变量,并从PHP脚本查询它。

I hope I am not forced to write the errors into a temporary table. Is there a better way?

我希望我不会*将错误写入临时表中。有更好的方法吗?

UPDATE: If possible, it would be very preferable if I could determine WHAT errors occurred, not simply IF one occurred.

更新:如果可能的话,如果我可以确定发生了什么错误,而不仅仅是发生了错误,那就更好了。

4 个解决方案

#1


1  

Try using shell_exec

尝试使用shell_exec

$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file" );
// parse $output here for errors

From the manual:

从手册:

shell_exec — Execute command via shell and return the complete output as a string

shell_exec -通过shell执行命令,并以字符串形式返回完整的输出

Note:

注意:

This function is disabled when PHP is running in safe mode.

当PHP在安全模式下运行时,此函数将被禁用。

EDIT: Full solution:

编辑:完整的解决方案:

what you need to do is grab STDERR and discard STDOUT. Do this by adding '2>&1 1> /dev/null' to the end of your command.

您需要做的是抓取STDERR并丢弃STDOUT。通过在命令的末尾添加“2>&1 > /dev/null”来实现这一点。

$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file 2>&1 1> /dev/null" );
$lines = explode( PHP_EOL, $output );

$errors = array();

foreach( $lines as $line )
{
    if ( strtolower( substr( $line, 0, 5 ) ) == 'error' )
    {
        $errors[] = $line;
    }
}

if ( count( $errors ) )
{
    echo PHP_EOL . 'Errors occurred during import.';
    echo implode( PHP_EOL, $errors );
}
else
{
    echo 'No Errors' . PHP_EOL;
}

#2


2  

$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname"
  . " < $file 2>&1";
exec($command, $output);

The error message you're looking for is probably printed to stderr rather than stdout. 2>&1 causes stderr to be included in stdout, and as a result, also included in $output.

您正在查找的错误消息可能打印到stderr而不是stdout。2>和1使stderr包含在stdout中,因此,也包含在$output中。

Even better, use proc_open instead of exec, which gives you far more control over the process, including separate stdout and stderr pipes.

更好的是,使用proc_open而不是exec,这将使您对流程有更多的控制,包括单独的stdout和stderr管道。

#3


1  

When issuing a exec, the shell will return a 0 on succes, or a number indicating a failure.

当发出exec时,shell将在succes上返回一个0,或者一个表示失败的数字。

$result = exec( $command, $output );

$result = exec($命令,$output);

should do the trick. Check result and handle appropiate.

应该足够了。检查结果并处理批准。

#4


-1  

You have done everything but look at the PHP manual! There is an additional parameter for the exec to return a result

除了PHP手册之外,您已经完成了所有工作!exec还有一个额外的参数来返回结果

http://php.net/manual/en/function.exec.php

http://php.net/manual/en/function.exec.php

"If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable."

“如果return_var参数与输出参数一起出现,那么被执行的命令的返回状态将被写到这个变量中。”

exec($command,$output,$result);
if ($result === 0) {
 // success
} else {
 // failure
}

#1


1  

Try using shell_exec

尝试使用shell_exec

$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file" );
// parse $output here for errors

From the manual:

从手册:

shell_exec — Execute command via shell and return the complete output as a string

shell_exec -通过shell执行命令,并以字符串形式返回完整的输出

Note:

注意:

This function is disabled when PHP is running in safe mode.

当PHP在安全模式下运行时,此函数将被禁用。

EDIT: Full solution:

编辑:完整的解决方案:

what you need to do is grab STDERR and discard STDOUT. Do this by adding '2>&1 1> /dev/null' to the end of your command.

您需要做的是抓取STDERR并丢弃STDOUT。通过在命令的末尾添加“2>&1 > /dev/null”来实现这一点。

$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file 2>&1 1> /dev/null" );
$lines = explode( PHP_EOL, $output );

$errors = array();

foreach( $lines as $line )
{
    if ( strtolower( substr( $line, 0, 5 ) ) == 'error' )
    {
        $errors[] = $line;
    }
}

if ( count( $errors ) )
{
    echo PHP_EOL . 'Errors occurred during import.';
    echo implode( PHP_EOL, $errors );
}
else
{
    echo 'No Errors' . PHP_EOL;
}

#2


2  

$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname"
  . " < $file 2>&1";
exec($command, $output);

The error message you're looking for is probably printed to stderr rather than stdout. 2>&1 causes stderr to be included in stdout, and as a result, also included in $output.

您正在查找的错误消息可能打印到stderr而不是stdout。2>和1使stderr包含在stdout中,因此,也包含在$output中。

Even better, use proc_open instead of exec, which gives you far more control over the process, including separate stdout and stderr pipes.

更好的是,使用proc_open而不是exec,这将使您对流程有更多的控制,包括单独的stdout和stderr管道。

#3


1  

When issuing a exec, the shell will return a 0 on succes, or a number indicating a failure.

当发出exec时,shell将在succes上返回一个0,或者一个表示失败的数字。

$result = exec( $command, $output );

$result = exec($命令,$output);

should do the trick. Check result and handle appropiate.

应该足够了。检查结果并处理批准。

#4


-1  

You have done everything but look at the PHP manual! There is an additional parameter for the exec to return a result

除了PHP手册之外,您已经完成了所有工作!exec还有一个额外的参数来返回结果

http://php.net/manual/en/function.exec.php

http://php.net/manual/en/function.exec.php

"If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable."

“如果return_var参数与输出参数一起出现,那么被执行的命令的返回状态将被写到这个变量中。”

exec($command,$output,$result);
if ($result === 0) {
 // success
} else {
 // failure
}