How read data from file and execute to MYSQL?
I create form to load sql file and used fopen function top open file and read this but when want to execute this data to database not w开发者_C百科ork? What is wrong in my code?
$ofile = trim(basename($_FILES['sqlfile']['name']));
$path = "sqlfiles/".$ofile;
//$data = settype($data,"string");
$file = "";
$connect = mysql_connect('localhost','root','');
$selectdb = mysql_select_db('files');
if(isset($_POST['submit']))
{
if(!move_uploaded_file($_FILES['sqlfile']['tmp_name'],"sqlfiles/".$ofile))
{
$path = "";
}
$file = fopen("sqlfiles/".$ofile,"r") or exit("error open file!");
while (!feof($file))
{
$data = fgetc($file);
settype($data,"string");
$rslt = mysql_query($data);
print $data;
}
fclose($file);
}
maybe you should use fgets instead of fgetc while youre reading lines.
Your script assumes that there is exactly one SQL statement per line, with no blanks or comments. If this is not the case then you'll need to either read and parse the file manually, or redirect the file to the mysql
executable and let it handle the SQL statements.
精彩评论