HektoR Posted November 14, 2008 Share Posted November 14, 2008 hello everyone. i have one question. i want to run mysql query from txt file. i have txt file on ftp and in it written: Code: [select] ("INSERT INTO users (name,lastname) VALUES ('xxx','yyy')"); Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/ Share on other sites More sharing options...
Lee-Bartlett Posted November 14, 2008 Share Posted November 14, 2008 Change file name to php? i am new so might be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-690392 Share on other sites More sharing options...
The Little Guy Posted November 14, 2008 Share Posted November 14, 2008 could you elaborate a little more please? Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-690400 Share on other sites More sharing options...
HektoR Posted November 14, 2008 Author Share Posted November 14, 2008 i want to insert data in mysql from txt file, wich are on my site. Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-690401 Share on other sites More sharing options...
The Little Guy Posted November 14, 2008 Share Posted November 14, 2008 you will need to use fopen and fread then you can convert the mysql to a PHP query and run the mysql_query function on it. Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-690407 Share on other sites More sharing options...
flyhoney Posted November 14, 2008 Share Posted November 14, 2008 <?php $query = file_get_contents('sql.txt'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-690413 Share on other sites More sharing options...
HektoR Posted November 16, 2008 Author Share Posted November 16, 2008 it did not work. please help me Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691181 Share on other sites More sharing options...
wildteen88 Posted November 16, 2008 Share Posted November 16, 2008 Whats in stored in sql.txt? An actual sql query or is it just plain text? You'll need to be more specific, also post your code here too. Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691182 Share on other sites More sharing options...
HektoR Posted November 16, 2008 Author Share Posted November 16, 2008 in sql.txt : "INSERT INTO admins(user,pass) VALUES ('zzz','ccc')" "INSERT INTO admins(user,pass) VALUES ('xxx','yyy')" and i dont know how make php code :'( Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691184 Share on other sites More sharing options...
wildteen88 Posted November 16, 2008 Share Posted November 16, 2008 So you have multiple sql queries defined within sql.txt and each query is on its own line? In which case you can use $queries = array_map('trim', file('sql.txt')); foreach($queries as $query) { mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691185 Share on other sites More sharing options...
HektoR Posted November 16, 2008 Author Share Posted November 16, 2008 it not works. Query: "INSERT INTO admins(user,pass) VALUES ('hekt','123456')"; Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"INSERT INTO admins(user,pass) VALUES ('hekt','123456')"' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691189 Share on other sites More sharing options...
flyhoney Posted November 16, 2008 Share Posted November 16, 2008 Try this: <?php $queries = array_map('trim', file('sql.txt'), '"'); foreach($queries as $query) { mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691190 Share on other sites More sharing options...
HektoR Posted November 16, 2008 Author Share Posted November 16, 2008 again error Warning: Invalid argument supplied for foreach() Warning: array_map() Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691194 Share on other sites More sharing options...
flyhoney Posted November 16, 2008 Share Posted November 16, 2008 again error Warning: Invalid argument supplied for foreach() Warning: array_map() Yeah that was dumb of me to suggest that. I was thinking that the quotes around the query could be messing things up. Maybe this? <?php $queries = array_map('trim', file('sql.txt')); foreach($queries as $query) { // dumb way of stripping quotes off of beginning and quotes + semicolon off of the end. $query = substr($query,1,-2); mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691195 Share on other sites More sharing options...
HektoR Posted November 16, 2008 Author Share Posted November 16, 2008 it works, thank you very very very much , all ! Quote Link to comment https://forums.phpfreaks.com/topic/132756-solved-mysql_query-from-txt-file/#findComment-691209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.