Jump to content

linking MySQL database to PHP


Mackey18

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.