如何在PHP中从文件运行一组SQL查询? [英] How to run a set of SQL queries from a file, in PHP?

查看:117
本文介绍了如何在PHP中从文件运行一组SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些SQL查询是在一个文件(即query.sql),我想运行这些查询使用PHP的文件,我写的代码是行不通的,

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

它回应文件中的SQL查询,但在mysql_query()函数中引发错误...

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

推荐答案


  1. 不要使用 file >除非你真的想要逐行的数据。 file_get_contents()更好。

  2. $ query == file($ file_name); 我不认为你想在这里进行比较。
  3. mysql_query 将只能一次执行一个查询。您必须想出一些方法将您的查询分离出来并逐个运行。

  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.

这篇关于如何在PHP中从文件运行一组SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆