Jump to content

'SOURCE' command problem


jefftanner

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/84334-source-command-problem/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.