Mackey18 Posted September 20, 2009 Share Posted September 20, 2009 Hi guys, i'm following a tutorial and I've reached a stage where I am meant to link a database i have made to a PHP page. The code was this: $dbcnx = @mysql_connect("localhost", "root", "mypasswd"); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } For starters is this correct? And secondly I don't have a password because It is a local so what should I put in that slot? Thank you, I'm an ultimate newbie lol Thanks again! Quote Link to comment Share on other sites More sharing options...
peranha Posted September 20, 2009 Share Posted September 20, 2009 Yes that code is correct. "localhost" is the server. "root" is the user name to the sql server "mypasswrd" is the password for the "root" user on the "localhost" server. Do you have MySQL set up on your computer? if so localhost is correct for the server. when you set it up, you needed to specify a username and a password. Those can be used for the user and password fields. If you dont have MySQL set up on your local computer, then the "localhost" server will not be correct and will need to be changed. Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 20, 2009 Author Share Posted September 20, 2009 I do have MySQL set up on my Mac, I think... I have already created a database so that must mean I have set it up, right? When I log into it on CommandPrompt/Terminal I don't type in a password All I do is say that the username is root. Quote Link to comment Share on other sites More sharing options...
BioBob Posted September 20, 2009 Share Posted September 20, 2009 Yep. Two bits of advice here. #1: The @ symbol is used as ERROR SUPPRESSION. So if the function kicks back an error, but its not a FATAL error (like a syntax error) you wont get any warning as to why the function failed to do what it was expected to do. #2: Logging in from a command prompt is just "mysql -u root" and if you want to give it a password, use "mysql -u root -p" and dont put the password in, it will prompt you for your password (if you have one) when you hit enter. Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 20, 2009 Author Share Posted September 20, 2009 Thank you, what I want to know is when linking the database how do I not enter a password as I don't have one for Local MySQL. Or alternatively how can I set a password for MySQL? Thank you. Quote Link to comment Share on other sites More sharing options...
Zane Posted September 21, 2009 Share Posted September 21, 2009 how do I not enter a password as I don't have one don't put one mysql_connect("localhost","username",""); notice the password field is blank... yep Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 21, 2009 Author Share Posted September 21, 2009 Thank you! Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 21, 2009 Author Share Posted September 21, 2009 Sadly I still can't connect even when I use that code! Quote Link to comment Share on other sites More sharing options...
Zane Posted September 21, 2009 Share Posted September 21, 2009 method 2: finding out why it doesn't work mysql_connect("localhost","username","") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 21, 2009 Author Share Posted September 21, 2009 mysql_connect("localhost","username","") or die(mysql_error()); Shall i put this in a PHP file? My username is root, right? Because it's on a local server?? Quote Link to comment Share on other sites More sharing options...
Zane Posted September 21, 2009 Share Posted September 21, 2009 what do you mean put it in a PHP file? use it the same way as you did the first statement. normally your username is root to begin with yeah Quote Link to comment Share on other sites More sharing options...
Mackey18 Posted September 21, 2009 Author Share Posted September 21, 2009 Ok thanks, I meant input this into the source of a webpage in php or html? Quote Link to comment 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.