Jump to content

[SOLVED] mysql_query from txt file


HektoR

Recommended Posts

So you have multiple sql queries defined within sql.txt and each query is on its own line? In which case you can use

$queries = array_map('trim', file('sql.txt'));

foreach($queries as $query)
{
     mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error());
}

it not works.

Query: "INSERT INTO admins(user,pass) VALUES ('hekt','123456')";
Error: 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 '"INSERT INTO admins(user,pass) VALUES ('hekt','123456')"' at line 1

again error

Warning: Invalid argument supplied for foreach()
Warning: array_map()

 

 

Yeah that was dumb of me to suggest that.  I was thinking that the quotes around the query could be messing things up.  Maybe this?

 

<?php
$queries = array_map('trim', file('sql.txt'));

foreach($queries as $query)
{
     // dumb way of stripping quotes off of beginning and quotes + semicolon off of the end.
     $query = substr($query,1,-2);
     mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error());
}
?>

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.