lukerodham Posted May 26, 2011 Share Posted May 26, 2011 Hi, this is more of a question than asking for help so if its in the wrong section could someone please move. my question is could you connect to a database by using an array that has been generated from getting contents from a file, for example... $file_c = file_get_contents("example.txt"); $colms = explode(",",trim($file_c)); mysql_connect("$colms[0]","$colms[1]","$colms[2]") or die(mysql_error()); mysql_select_db("$colms[3]") or die(mysql_error()); if not how else would someone go about doing this. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2011 Share Posted May 26, 2011 Array variables are no different than any other variable, as long as they contain what you expect. What sort of problem or error did you have when you tried your code? Be careful when storing database credentials in a plain text file as anyone who discovers that file or sees it if your server allows directory listings can simply browse to the file and see your database credentials. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220330 Share on other sites More sharing options...
gizmola Posted May 26, 2011 Share Posted May 26, 2011 Yes of course. Variables are variables and mysql_connect() simply requires variables to plug into it's parameters when it is called. It doesn't care one bit whether or not those variables are elements in an array. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220331 Share on other sites More sharing options...
lukerodham Posted May 26, 2011 Author Share Posted May 26, 2011 hey guys thanks for the quick response. i thought that this would be the case im getting this error: Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'localhost ' (11004) but i dont understand why. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220335 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2011 Share Posted May 26, 2011 You have a space or some other white-space character on the end of the localhost value. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220336 Share on other sites More sharing options...
lukerodham Posted May 26, 2011 Author Share Posted May 26, 2011 ive just looked at the file where the value is held and there is not whitespace in front of the value and no behind. i do have the comma there that is trimmed but thats it. could there be a problem with the file_get_contents action that i have written? Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220338 Share on other sites More sharing options...
jcbones Posted May 26, 2011 Share Posted May 26, 2011 Run trim() on the data. That should get rid of the whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220340 Share on other sites More sharing options...
lukerodham Posted May 26, 2011 Author Share Posted May 26, 2011 ok thank you i'll try this. Quote Link to comment https://forums.phpfreaks.com/topic/237486-mysql_connect-question/#findComment-1220341 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.