PHP script to import file in MySQL

风格不统一 提交于 2019-12-08 10:36:05

问题


I have some code in my postwwwacct script which doesn't work, it doesn't import the sql file

shell_exec("cd /home/".$opts['user']."/public_html/sbans/sql/");
$command="mysql -u ".$opts['user']." -p".$opts['pass']." ".$opts['user']."_bans < data.sql";
shell_exec($command);

However after account creation i can manually run in ssh

cd /home/jason/public_html/sbans/sql/
mysql -u jason -pmypass jason_bans < data.sql

Which then works.

What is the problem with the php code in postwwwacct?

Sorry for re-editing your edit but the file does not have php extension i manually call the php compiler by using

#!/usr/local/bin/php -q

回答1:


the problem is that shell_exec does cd and finishes so the command which would do the import with the sql is not found and failed.

try something like this :

<?php
$command="mysql -u ".$opts['user']." -p".$opts['pass']." ".$opts['user']."_bans < data.sql";
$output = shell_exec("cd /home/".$opts['user']."/public_html/sbans/sql/ && ".$command);

and if you want to see the result of your command just do

echo $output




回答2:


Off the top of my head, are you sure the file data.sql is in all the different user's sql folders? You could also create a seperate bash script and call it in your script, that worked for me before, you just pass in the parameters with something like this:

$t = shell_exec("path/command.sh '$opts['user']' '$opts['pass']' 2>&1");
echo $t

and in the bash script start off like this to get the variables: #!/bin/bash

 #Get variables needed

 user=$1
 pass=$2

You can then proceed to use the variables as any normal bash variables, and if it is still not working for you becouse of the 2>&1 at the end of your shell_exec command you'll be able to debug it alot easier.

Good luck!



来源:https://stackoverflow.com/questions/12072479/php-script-to-import-file-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!