php - read and execute commands from text file -
i have txt file sql commands. need open file, read commands , execute against postgresql.
$file = fopen("createme.sql.txt", "r") or exit("unable open file!"); while(!feof($file)) { pg_query($conn, fgets($file)); } fclose($file);
i expected above code work. shows error:
php warning: pg_query(): query failed:
how execute commands text file?
fgets
file content line line.
using fread
content of file, set second parameter size of files.
$filename = "createme.sql.txt"; $file = fopen($filename, "r") or exit("unable open file!"); // checking if opening file error. if(!feof($file)) { $str = fread($file, filesize($filename)); pg_query($conn, $str); } fclose($file);
another trick @smassey
$filename = "createme.sql.txt"; $str = file_get_contents($filename); pg_query($conn, $str); fclose($file);
Comments
Post a Comment