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());
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.