导入 phpmyadmin 生成的 sql 转储
我想使用 php 以编程方式从 phpMyAdmin 生成的 sql 转储中导入数据。
我发现了一些问题,但它们没有帮助我,因为我没有 shell 访问权限,并且没有由于外键而导致的问题限制。
使用 kohana 从 phpmyadmin 生成的 SQL 转储创建表
----编辑----
这是我正在使用的代码,它工作正常,但我认为当数据库中有一些 html 时它会崩溃。
sqldump_to_statements($contents)
{
// remove c style and inline comments with -- and #
$comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/','/\s*--.*\n/','/\s*#.*\n/');
$contents = preg_replace($comment_patterns, "\n", $contents);
$statements = explode(";\n", $contents);
$statements = preg_replace("/\s/", ' ', $statements);
return $statements;
}
I want to import data from sql dump generated by phpMyAdmin programmatically using php.
I found some questions but they did not helped me because i don't have shell access and there is no problems due to foreign-key constraints.
Create tables from SQL dump generated by phpmyadmin using kohana
Programmatic Equivalent Of Import in phpMyAdmin
----EDIT----
This is code i am using, it works fine but i think it breaks when there is some html in database.
sqldump_to_statements($contents)
{
// remove c style and inline comments with -- and #
$comment_patterns = array('/\/\*.*(\n)*.*(\*\/)?/','/\s*--.*\n/','/\s*#.*\n/');
$contents = preg_replace($comment_patterns, "\n", $contents);
$statements = explode(";\n", $contents);
$statements = preg_replace("/\s/", ' ', $statements);
return $statements;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以当前格式回答问题的问题是可以做出两种解释。
登录后,选择要使用的数据库,然后在页面顶部附近有一个标记为
import
的选项卡,只需单击该选项卡并按要求上传文件,然后等待 PHPMyAdmin 执行操作这是工作从这里您有两种选择,您可以手动将文件拆分为多个部分,即一个文件用于所有表创建查询,另一个文件用于所有 INSERT 查询。这里的第二个选择是处理整个文件,因为它使用 PHP 查询,您可以将整个文件包含在脚本中,然后可能考虑将文件拆分为一个数组,以便更轻松地处理,并让最终用户知道如何处理正在去。
编辑:我知道它很啰嗦,如果您需要它来帮助我可以查看提供一些代码示例
The issue with answering the question in it's current format is that there is two interpretations that could be made.
Once you have logged in, select the database to be used then near the top of the page you have a tab marked
import
, just click on that and upload the file as requested and sit back for PHPMyAdmin to do it's jobFrom here you have two choices, you can either manually split the file into parts, i.e. one file for all of the table creation queries, a different file for all the INSERT queries. The second choice here is to process the whole file as it is using PHP queries, you can include the file as whole in your script then possibly look at splitting the file into an array to process easier and to let the end user know how the process is going.
Edit: I know it is wordy and if you needed it to help I could look at providing some code examples
我不确定这是否是您想要的,但您可以再次使用 phpMyAdmin 导入转储。获得一些额外的信息会很好。
选择您想要导入的数据库。
选择导入选项卡。
从那里您可以选择文件并定义其他参数。
希望这有帮助。
I am not sure whether this is what you want, but you could just use phpMyAdmin again to import the dump. It would be good to get some additional information.
Select the database you want to import into.
Select the Import tab.
And from there you can select your file and define other parameters.
Hope this helps.
使用 phpMyAdmin:
1:确保您要导入的数据库已创建并清除干净,但是如果您只是恢复数据库,请记住删除所有表。您可以使用表列表底部下拉框中的 DROP 选项与所有选定的表相结合。
2:选择您的数据库
3:单击屏幕顶部顶部菜单中的“导入”链接。您将看到一个文本框区域和一个文件上传选项。
4:单击文件上传选项上的浏览并找到您的 .sql 文件。然后单击“开始”按钮开始该过程。
注意:如果您在上传时遇到任何问题,很可能是因为您的 .sql 文件的大小,因为导入的大小非常有限,不用担心,您需要进行的更改很少添加到你的 php.ini 后应该就可以了。
1- 找到 php.ini 配置文件,并将 upload_max_filesize、memory_limit 和 post_max_size 的值更改为大于 .sql 文件的值。
2- 重新启动您的 apache 服务器并重试。
还有第二种使用 MySQL 客户端 GUI 的方法,该方法更简单,请访问此链接以获取完整的详细信息 http://www.alfasky.com/2008/04/how-to-backup-and-restore-a-mysql-database/
Using phpMyAdmin:
1: Make sure that the database you are importing into is created and wiped clean, however if you are just restoring your database remember to drop all your tables. You can use the DROP option from the dropdown box at the bottom of your tables list combined with all selected tables.
2: Select you database
3: Click the Import link from the top menu at the top of your screen. You will see a Text Box area and a file upload option.
4: Click browse on the file upload option and find your .sql file. Then click the ‘GO’ button to start the process.
Note: If you encounter any problem while uploading, it’s most likely because of the size of you .sql file as the importing size is very limited, don’t worry there is a very few changes that you need to add to your php.ini and it should be ok after.
1- Locate your php.ini configuration file and change the values of upload_max_filesize, memory_limit and post_max_size to larger sizes than your.sql file’s one.
2- Restart your apache server and try again.
There is a second method using MySQL client GUI wich a lot easier please visit this link for the complete detail http://www.alfasky.com/2008/04/how-to-backup-and-restore-a-mysql-database/