Jump to content

need Help connecting using a function.


srhino

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Sorry :D 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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.