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

查看:31
本文介绍了从 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.大多数共享主机也不允许您使用 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天全站免登陆