Help On Connecting To Mysql Db

problems trying to connect to my DB

Page 1 of 1

4 Replies - 2022 Views - Last Post: 13 December 2004 - 10:09 AM Rate Topic: -----

#1 Hellsbellboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-June 03

Help On Connecting To Mysql Db

Posted 10 December 2004 - 11:00 PM

<html>
<head> <title>MySQL Connection Test</title> </head>
<body>
<h2>
<?php
$connection = mysql_connect("localhost","username","password")
or die("Sorry - unable to connect to MySQL");
echo ("Congratulations - you connected to MySQL");
?>
</h2>
</body>
</html>


I'm trying to connect to my website's MySQL database, but I'm not sure how to.. I was following a example in one of my PHP Tutorial Books, but I'm not sure what to put for my Localhost. I have a website, hellsbellboy.com and it has 2 MySQL databases, has Apache and all that.. but I'm not sure what I'm suppose to put in place for Localhost.. I thought it was suppose to be www.hellsbellboy.com, then my username and password but that doesn't work.

this is the error i get when i try and check it:

Warning: mysql_connect(): Access denied for user: 'hellsbel_@localhost' (Using password: YES) in /home/hellsbel/public_html/mysqltest.php on line 6
I cannot connect to the database because: Access denied for user: 'hellsbel_@localhost' (Using password: YES)


http://www.hellsbell...m/mysqltest.php :crazy:

Thank you for any help.

This post has been edited by Hellsbellboy: 10 December 2004 - 11:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help On Connecting To Mysql Db

#2 richowe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-May 04

Re: Help On Connecting To Mysql Db

Posted 11 December 2004 - 02:58 AM

Some Assumptions First:

1.) Your site and database reside on the same machine.
2.) You haven't created any new user accounts for MySQL and only have the default 'root' account available
3.) You haven't reset the 'root' users password

I won't lecture you about securing your database here - we'll save that for another discussion.

To get you up and running quickly, since you've installed a MySQL database (or have one), you probably haven't reset the root user's password so you will use the default setting to connect to your database:

$conn = mysql_connect('localhost', 'root', '') or die ("Can't Connect To Database");
$db = mysql_select_db("database_name", "$conn") or die ("Can't Select Database");



1.) 'localhost' is your computers way of referencing itself so there's no imperative need to use a formal host name (e.g. mydomainname.com). If, however, the database resides on a server apart from your web site, you will then use the host name (or ip address) of the database server.

2.) Substitute your real database name for 'database_name' in the above example.

---------------------------

Now you are set to query your database

$sql = "SELECT * FROM tablename WHERE tablename_id = $var;";
$result = mysql_query($sql) or die (mysql_error());
$rows = mysql_num_rows($result);

if ($numrows > 0) {
     while ($var_name = mysql_fetch_array($result)) {
          ---- do something with your data here ----
     }
}


This post has been edited by richowe: 11 December 2004 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1858
  • View blog
  • Posts: 20,275
  • Joined: 17-March 01

Re: Help On Connecting To Mysql Db

Posted 11 December 2004 - 12:11 PM

Not sure if this will help or not, but here is a tutorial I wrote on basic PHP and mySQL:

http://forums.dreami...?showtopic=9772
Was This Post Helpful? 0
  • +
  • -

#4 Hellsbellboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 28-June 03

Re: Help On Connecting To Mysql Db

Posted 12 December 2004 - 02:56 PM

ugh i don't know what my problem is but still haven't problems.. :crazy:

Thank you for the help
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1858
  • View blog
  • Posts: 20,275
  • Joined: 17-March 01

Re: Help On Connecting To Mysql Db

Posted 13 December 2004 - 10:09 AM

If you are still getting the access denied error, have your webhost reset your mySQL password and double check your permissions on the database.

It doesn't appear to be a problem with your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1