srhino Posted July 27, 2012 Share Posted July 27, 2012 I keep getting this error when I try to load my page. I cant figure out why it wont use the function "login". I have attached some of the code as well Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'XXXXXXX.db.9999999.hostedresource.com' (110) in /home/content/s/r/h/xxxx/html/softball/mylibrary/login.php on line 4 Could not connect to server Here is where on the index page the function is called... <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> <link rel="stylesheet" media="print" type="text/css" href="print.css" /> <title></title> </head> <?php include("mylibrary/login.php"); include("mylibrary/showproducts.php"); include("mylibrary/getThumb.php"); login(); ?> and here is where I defined the function <?php function login() { $con = mysql_connect("xxxxxxx.db.999999.hostedresource.com", "gxxxxxx", "xxxxxx") or die('Could not connect to server'); mysql_select_db("xxxxxx", $con) or die('Could not connect to database'); } ?> Thanks in advance for any help and explanation of why this would happen...I am new to this and am trying to learn. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/ Share on other sites More sharing options...
DavidAM Posted July 27, 2012 Share Posted July 27, 2012 It is using the login() function. That error message is coming from that function. The mysql_connect() is failing. Probably the hostname is incorrect. It might be the username or password, but I think the error message is different for those. Is this database server on the same machine as the php script? If so, you can try "localhost". Or ask your host (admin) to see what the connection parameters are supposed to be. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364965 Share on other sites More sharing options...
srhino Posted July 27, 2012 Author Share Posted July 27, 2012 I thought that was the problem too...But as soon as I removed the function and used it as an include...it connected fine. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364966 Share on other sites More sharing options...
DavidAM Posted July 27, 2012 Share Posted July 27, 2012 But as soon as I removed the function and used it as an include...it connected fine. Removed the function from where? Used what as an include? I don't understand what you are saying. Is it working now? The error message tells you the file name and line number where the error occurred. The error message you posted specifically said: in /home/content/s/r/h/xxxx/html/softball/mylibrary/login.php on line 4 login.php is the first included file in the index page you showed. Line 4 of "here is where I defined the function" is the mysql_connect() function call. So it looks like the login() function is in the login.php file. Is that true or not? Are you creating a second login function? You can't have two functions with the same name, but that would be a different error message. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364969 Share on other sites More sharing options...
srhino Posted July 27, 2012 Author Share Posted July 27, 2012 Sorry If you look below in my code you see I wrote the function in the "mylibrary folder" the code below is in my index...I <?php include("mylibrary/login.php"); include("mylibrary/showproducts.php"); include("mylibrary/getThumb.php"); login(); <------------------------------------I removed this call to the function and it worked. ?> I guess it works, but i would like to be able to re use the login when needed. login.php is the first included file in the index page you showed. Line 4 of "here is where I defined the function" is the mysql_connect() function call. So it looks like the login() function is in the login.php file. Is that true or not? Yes that is true. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364975 Share on other sites More sharing options...
DavidAM Posted July 28, 2012 Share Posted July 28, 2012 it worked. Is a meaningless statement in this industry. Or perhaps I should say ambiguous. If you take out the call to the login() function ... "it works" -- You see your full page in all its glory with all the database data that is supposed to be there? "it works" -- You see your page without any error messages, but you haven't written the database parts yet so there is no data displayed? If you are getting database data without calling login(), then some other file is connecting to the database. And it must be using different connection parameters since it isn't throwing any errors. If you just haven't written the stuff to retrieve data from the database (which is what I am guessing the "showproducts.php" file will eventually do), then you really do need to get the login() function working. You are right, you want a function in an include file, that you can use on any page-script that needs database access. Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364991 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2012 Share Posted July 28, 2012 It would be helpful to know what the error from MySQL was also. <?php function login() { //$con = mysql_connect("xxxxxxx.db.999999.hostedresource.com", "gxxxxxx", "xxxxxx") or die('Could not connect to server'); <--- COMMENT OUT THIS LINE $con = mysql_connect("xxxxxxx.db.999999.hostedresource.com", "gxxxxxx", "xxxxxx") or die( mysql_error() ); // <--- ADD mysql_select_db("xxxxxx", $con) or die('Could not connect to database'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266359-need-help-connecting-using-a-function/#findComment-1364994 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.