jefftanner Posted January 3, 2008 Share Posted January 3, 2008 I am having a problem with MySQL 'SOURCE' command when called through PHP 5.1 mysqli instance. PHP Version 5.1.4, mysqli module: Client API library version 5.0.38 MySQL Version 5.1.22-rc-community My SQL command is "SOURCE prep_angelfish.sql", which works through the MySQL command prompt, but not when called through a PHP/mysqli query. Calling from MySQL command prompt: mysql> SOURCE prep_angelfish.sql; Query OK, 0 rows affected (0.00 sec) Calling through PHP: $target_dbh->query("SOURCE prep_angelfish.sql") Query error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE prep_angelfish.sql' at line 1. $sql_file = "prep_angelfish.sql"; if ( !file_exists($sql_file) ) { print "Error: $sql_file does not exist!\n"; exit (1); } @$target_dbh = new mysqli($target_db['host'], $target_db['user'], $target_db['passwd']); if (mysqli_connect_errno()) { print "Connect failure: Target host " . $target_db['host'] . " connection error: (" . mysqli_connect_errno() . ") " . mysqli_connect_error() . "\n"; exit(mysqli_connect_errno()); } $mysql_query = "SOURCE $sql_file" ; if ( $target_dbh->query($mysql_query) !== TRUE ) { print "Query failure on: " . $target_db['host'] . "\n"; print "Query: \"$mysql_query\"\n"; print "Query error: (" . $target_dbh->errno . ") " . $target_dbh->error . ".\n"; exit($target_dbh->errno); } Thanks, any ideas??? Jeff in Seattle Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/ Share on other sites More sharing options...
fenway Posted January 3, 2008 Share Posted January 3, 2008 If you have access to the CLI, you can just redirect it in. Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/#findComment-429713 Share on other sites More sharing options...
effigy Posted January 3, 2008 Share Posted January 3, 2008 I think SOURCE is only known to the CLI. Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/#findComment-429719 Share on other sites More sharing options...
jefftanner Posted January 3, 2008 Author Share Posted January 3, 2008 If you have access to the CLI, you can just redirect it in. Explain resolving problem by 'redirect'? Is this what you mean?: $mysql_cmd = "mysql -u {$target_db['user']} -p{$target_db['passwd']} -h {$target_db['host']}"; $sql_file = "prep_angelfish.sql"; $last_line = system($mysql_cmd . " < " . $sql_file, $return_value); if ( $last_line === FALSE ) { exit ($return_value); } I would prefer to avoid this approach since its error report from MySQL would be muted. Thanks, Jeff in Seattle Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/#findComment-429726 Share on other sites More sharing options...
fenway Posted January 4, 2008 Share Posted January 4, 2008 One would hope that PHP would have a way to capture STDOUT / STDERR from a system call ;-( Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/#findComment-430373 Share on other sites More sharing options...
effigy Posted January 4, 2008 Share Posted January 4, 2008 exec might do it: string exec ( string $command [, array &$output [, int &$return_var ]] ) You can combine STDERR and STDOUT by adding 2>&1 at the end of your command. Quote Link to comment https://forums.phpfreaks.com/topic/84334-source-command-problem/#findComment-430388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.