# php and MySQL...having issues with connection



## daneh (Aug 20, 2007)

Hi All,
I am very new to computers and this is my first post. I am designing a website and want to be a little more interactive hence trying to learn php and for databases...MySQL. I do not have anything installed on my drive, however I am using Godaddy 's server ( I am on Linux). 

My problem? I cannot get a connection. I am using this piece of code to initiate the connection:
<?php
//Sample Database Connection Syntax for PHP and MySQL.

//Connect To Database
$hostname="mysql.secureserver.net";
$username="your_dbusername";
$password="your_dbpassword";
$dbname="your_dbusername";
$usertable="your_tablename";
$yourfield = "your_field";
mysql_connect($hostname,$username, $password);
mysql_select_db($dbname);

# Check If Record Exists

$query = "SELECT * FROM $usertable";

$result = mysql_query($query);

if($result)
{
while($row = mysql_fetch_array($result))
{
$name = $row["$yourfield"];
echo "Name: ".$name."
";
}
}
?>

I get nothing. Of course I enter my password and username. I have been on the phone with tech support at Godaddy, however they do not offer support for the actual software operation. 

I also tried a piece of code from a textbook( Larry Ullman's PHP for the World Wide Web):
<?php
ini_set ('display_errors',1);
error_reporting (E_ALL & ~E_NOTICE);
if ($dbc = mysql_connect ('mysql.secureserver.net', 'username', 'password'))
{print '<p>Successfully connected.</p>';
mysql_close();
}
esle{ print'<p>Could not connect.'</p>;
}
?>
No success with that either. The error message I keep getting is "unknown Host".

Has anybody had this issue. I tried downloading MySQL onto my drive but could not get it to activate ( I have an Apache2 server installed already). I am very anxious to start scripting and learning more. I thank you for your help in advance.
Dane


----------



## Redcore (Aug 14, 2007)

First off, let me give you a very good PHP/MySQL tutorial site that I reference a lot:
http://www.php-mysql-tutorial.com/index.php

You should tell it to die and give you an error if it doesnt work...like this:


```
mysql_connect("$hostname", "$username", "$password") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
```
Let me know what errors it spits out. You shouldn't have to use that piece of code below if you just tell it to die and show you the error.


----------



## daneh (Aug 20, 2007)

This is the error message I got:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2) in /home/content/d/n/s/dnscw/html/mysql.php on line 10
Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2)
I went to the tutorial and found that it is really only for people who have the software installed on their computer...I am using a remote server(Godaddy)


----------



## Redcore (Aug 14, 2007)

Who setup your database on GoDaddy? They should have something like phpMyAdmin (most web hosts do).


----------



## daneh (Aug 20, 2007)

Yes they do have that. I set up a test database to work with. Is phpmy admin the same as php?

Also I tried unsuccessfully to download MySQL and the GUi software and could not get it to connect or initiate if you will. There was a lot of configuring and I may have done something wrong there. I did have an IT guy look at it and he couldn't get it to connect either.


----------



## Redcore (Aug 14, 2007)

phpMyAdmin is just a PHP program that you can use to run MySQL databases...like a web-based MySQL GUI.

Does GoDaddy give you cPanel? My only guess is that you haven't created a username/password to access the database and are just using the username/password that is supposed to be for accessing phpMyAdmin.


----------



## daneh (Aug 20, 2007)

I can't find anything about a cPanel, and I think you're right about logging in to myphpadmin and not to MySQL. Having said that, I see nowhere to create a password for MySQL. I thought the password I created in the beginnign was what I needed. 

Godaddy is really no help whatsoever. I will give them a call and see what gives...AGAIN


----------



## daneh (Aug 20, 2007)

You know Redcore, I think the password is OK for MySQL...went back and checked the initail database creation and it calls for the username and passowrd to be created at the database level. 

I realy want to have all this on my computer, however I cannot get the MySQL software from a download to work. Where did you get your from and do you use GUI tools? Thanks


----------



## Redcore (Aug 14, 2007)

I just use the MySQL and phpMyAdmin installed on the server I rent.

Perhaps you should just delete the database and start from scratch. If it still doesn't work, the host address is probably wrong. I can't really say because I don't have any of the details so I'm just giving blind suggestions.


----------



## daneh (Aug 20, 2007)

I have done just that...deleted and created numerous new databases. I also called Godaddy and checked on the host name for running it on Linux instead of Windows...they have a piece of code for connecting to Linux. I have thought of using the IP address instead of the text given for the host name...any thoughts on that one? Thanks.


----------



## Redcore (Aug 14, 2007)

I would go with the IP address.


----------



## daneh (Aug 20, 2007)

Thanks Redcore...hope this works!!


----------



## paseywhitaker (Apr 9, 2009)

No one has added a solution to this thread so here it is:

The answer is in the error message you are receiving: "unknown host"

If you are using Godaddy you will need to point your mysql_connect address to your Godaddy hosting account I.P address:


```
$dbh=mysql_connect ("[B]10.6.166.223[/B]", "YOUR_USERNAME",
"YOUR_PASSWORD") or die('Cannot connect to the database because: ' . mysql_error());

mysql_select_db ("YOUR_DATABASE");
```
I had your exact problem and changing the host address to the I.P address resolves the issue.


----------



## samvel2609 (May 5, 2009)

MR paseywhitaker...even i encountered the same problem at godaddy... wen i tried to make a php/mysql connection....by Godaddy hosting account I.P address ????...wat do u mean??..can u explain me clearly?


----------

