tags:

views:

32

answers:

2

I have some set of SQL queries which is in a file(i.e query.sql), and i want to run those queries in files using PHP, the code that i have wrote is not working,

//database config's...
$file_name="query.sql";
$query=file($file_name);
$array_length=count($query);
for($i=0;$i<$array_length;$i++)
{
    $data .= $query[$i];
}

echo $data;    
mysql_query($data);

it echos the SQL Query from the file but throws an error at mysql_query() function...

A: 

If you ever bother yourself to read mysql_query() manual page, it says this function can execute only one query at once.
So, you have to put it inside of the loop

Col. Shrapnel
read some comments too -> http://lv.php.net/manual/en/function.mysql-query.php#91669
Anpher
ooh! yeah..right i just forgot....thanks @Col.Shrapnel to show my mistake...
Harish Kurup
+2  A: 
  1. Don't use file() unless you really want the data line-by-line. file_get_contents() is better.
  2. $query==file($file_name); I don't think you want to do a comparison here.
  3. mysql_query will only ever execute a single query at once. You'll have to come up with some way of separating your queries in the file and run them one-by-one.
Matti Virkkunen
thanks @Matti Virkkunen...
Harish Kurup