Jump to content

PHP connection problems to MySQL database


feck

Recommended Posts

Hi people,I use a mysql_connect() function to connect to a mysql database. When I actually input the true host unsernam and password into the function something like:$connection=mysql_connect('someHost', 'made_upName', 'secretPassword'); //note these are not my real valuesEverything works fine.but when I create an external file and refer to it using the 'require' function, I get an error message telling me that i cannot connect, using this line in my code.Heres my external file with the variable values missing:<?php$host = '':$user = '';$passwd = '';$dbname = '';?>And Ive named it mysql.phpthe require 'mysql.php';and the new connect function:$connect=mysql_connect($host, $user, $passwd);But as stated I'm doing something wrong, somewhere because it will not connect.Can anyone help?

Link to comment
Share on other sites

You have a : at the end of $host='' instead of ;If that doesn't fix it try troubleshooting one bit at a time.After the require function try printing out the variables egprint $host . "<br />";print $user . "<br />";print $passwd;exit;Just to make sure that the variables have been defined. Is mysql.php in the same folder as the main file? Maybe you have to navigate to a different folder eg require '../mysql.php'.Try the : / ; first then take it from there.

Link to comment
Share on other sites

It probably is the colon instead of a semicolon. I have had includes and requires act weird some times, like I couldn't include a url to the whole site, just files. maybe thats just my site!

Link to comment
Share on other sites

Thanks people,already solved it, for some reason may be its because i'm new at this, but when I used the connect variable inside one of my functions it would not work, and outside it would.Some times if somethings seems broken and I try and fix it, it remains broken.But if I break it some more, it fixes itIf that makes any sense.Thanks anyway

Link to comment
Share on other sites

Its because variable defined within a function are usually only available within the function unless you return it to the main program properly. You need to read about global variables at php.net. This is a common cause of problems - you know the variable has a value but not the one you expected.To show how it works run this little script.

<?phpprint "We will give x the value of 5<br />";$x=5;print "Let's print x to prove it.  x = $x<br />";print "Now we'll hand over to the function...<br />";xtest();print "We have returned from the function.  Will x be 5 or 10?<br />";print "Let's print x to find out.  x = $x<br />";print "This shows that the value of x wasn't changed in the main<br />";print "program even though we gave it a different value inside the function.";function xtest()	  {	  print "Inside the function we will give x the value of 10<br />";	  $x=10;	  print "Let's print x to prove it.  x = $x<br />";	  print "Now we'll return to the main program...<br />";	  }?>

Which gives this output:

We will give x the value of 5Let's print x to prove it. x = 5Now we'll hand over to the function...Inside the function we will give x the value of 10Let's print x to prove it. x = 10Now we'll return to the main program...We have returned from the function. Will x be 5 or 10?Let's print x to find out. x = 5This shows that the value of x wasn't changed in the mainprogram even though we gave it a different value inside the function.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...