从PHP中加载.sql文件 [英] Loading .sql files from within PHP

查看:92
本文介绍了从PHP中加载.sql文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在开发的应用程序创建一个安装脚本,需要从PHP中动态创建数据库。我有它来创建数据库,但现在我需要加载几个.sql文件。我曾计划打开文件,mysql_query一次一行 - 直到我查看架构文件并意识到它们不是每行一个查询。

I'm creating an installation script for an application that I'm developing and need to create databases dynamically from within PHP. I've got it to create the database but now I need to load in several .sql files. I had planned to open the file and mysql_query it a line at a time - until I looked at the schema files and realised they aren't just one query per line.

那么,我如何从PHP中加载一个sql文件(如phpMyAdmin的导入命令)?

So, how do I load an sql file from within PHP (as phpMyAdmin does with its import command)?

推荐答案

我是感觉这里的每个人都回答了这个问题并不知道成为一个允许人们在自己的服务器上安装应用程序的Web应用程序开发人员是什么感觉。特别是共享托管,不允许您使用SQL,如前面提到的LOAD DATA查询。大多数共享主机也不允许你使用shell_exec。

I'm getting the feeling that everyone here who's answered this question doesn't know what it's like to be a web application developer who allows people to install the application on their own servers. Shared hosting, especially, doesn't allow you to use SQL like the "LOAD DATA" query mentioned previously. Most shared hosts also don't allow you to use shell_exec.

现在,为了回答OP,你最好的办法是建立一个包含你的查询的PHP文件在变量中,可以运行它们。如果您决定解析.sql文件,您应该查看phpMyAdmin并获取一些从这种方式获取.sql文件数据的想法。查看具有安装程序的其他Web应用程序,您将看到它们,而不是使用.sql文件进行查询,它们只是将它们打包成PHP文件,然后通过mysql_query运行每个字符串或者它们需要做什么。

Now, to answer the OP, your best bet is to just build out a PHP file that contains your queries in a variable and can just run them. If you're determined to parse .sql files, you should look into phpMyAdmin and get some ideas for getting data out of .sql files that way. Look around at other web applications that have installers and you'll see that, rather than use .sql files for their queries, they just package them up in PHP files and just run each string through mysql_query or whatever it is that they need to do.

这篇关于从PHP中加载.sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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