...

View Full Version : newbie connect to mysql database



PonchoX
06-23-2010, 07:24 AM
hi.

learning php.

at part where we connect to database.

2 questions.

1. tutorial has me using "localhost" as 1st argument in mysql_connect, with wamp server on local machine. will i need to change this to my domain name when moving files to production site? .. or will "localhost" still work there?

2. tutorial has me establishing connection mentioned above using "root" user. then in step #2, they select the database. but wouldn't it be better to connect using the desired db, instead of root, by using login credentials for the desired db?

i have vps, and when i create new db in cpanel, i create user name and passwod for each particular database.

feels weird establishing a root connection. i always used to working with database-specific user name and password.

no where do i ever enter the db-specific user name and pw it seems.

do you follow me?

guidance?

i would prefer to user db-specific credentials, not root.

then it would seem like i don't have to specify the db in step #2. (db_select)

Keleth
06-23-2010, 09:00 AM
1. tutorial has me using "localhost" as 1st argument in mysql_connect, with wamp server on local machine. will i need to change this to my domain name when moving files to production site? .. or will "localhost" still work there?


Depends on your host. Some hosts allow localhost, others don't. They should tell you.


2. tutorial has me establishing connection mentioned above using "root" user. then in step #2, they select the database. but wouldn't it be better to connect using the desired db, instead of root, by using login credentials for the desired db?

Again, depend on the host if they'll let you use root, but regardless, you shouldn't use root, because if someone gets in, they'll be able to mess with all the dbs root has access to.

Even if you use a db specific username/pass, you'll still have to select the db... the server doesn't know what db you're trying to access when you enter a user/pass, just what dbs that user/pass have access to.

Craigy
06-23-2010, 01:24 PM
It might be something like



<?php
$username="cpannelusername";
$password="cpannelpassword";
$database="cpannelusername_databasename";
mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());


But like Keleth said, this depends on your host.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum