Page 1 of 1

MySql Connection

Posted: Sat Sep 01, 2012 2:45 pm
by Pavilion
Hello:

I've successfully established a few PDO/PHP connections to mySQL. All these connections were to databases stored on different hosting servers. Now - just as an exercise in learning - I am trying to establish a PDO/PHP connection to mySQL on my own system. I've successfully established a connection to my office hard drive, from mySQL Workbench installed on my laptop.. so I know it's doable.

But when I try to set a PDO/PHP connection I can't make a go of it. Following is my script:

Code: Select all

//Define Database
$hostname = '000.000.0.00'; // IP address
$dbname= 'test'; // name of schema I'm trying to connect to.
$username = 'root';
$password = 'password'; // password is correct

// Create PDO link to the database with the following "try" block
try 
{
$link = new PDO("mysql:host=$hostname;port=3306;dbname=$dbname", $username, $password);
echo "a link has been established to homebase.";
}
	// This block throws an error message if there is no connection. PDO uses "exceoptions" to handle errors.
	catch(PDOException $e) {
	echo "There is an error somewhere. I have no idea where, but there is an error.<br />";
	echo "The error: " . $e . "<br />";
	// The following echo will return php generated message. Use for stepping through an error.
	// echo $e->getMessage();
The returned error message is as follows:
The error: exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on '000.000.0.000' (110)' in /home/user/public_html/include/connect.php:15 Stack trace: #0 /home/user/public_html/include/connect.php(15): PDO->__construct('mysql:host=000....', 'root', 'password') #1 {main}
Any assistance in this area is appreciated.

Thanks in advance:

Pavilion

Re: MySql Connection

Posted: Sat Sep 01, 2012 3:02 pm
by Benjamin
hostname should be localhost

Re: MySql Connection

Posted: Sat Sep 01, 2012 8:35 pm
by Pavilion
Benjamin:

I tried localhost, and got error messages. I don't think it should be localhost anyway, because the php file is stored on my host server and not on my own system. So... the IP address makes logical sense.

I removed the quotes from around my IP address. And I'm not getting error messages anymore, but neither am I getting anything else. No echos, no feedback from my db when I run a SQL statement. My error log gives the following error message:
[01-Sep-2012 19:20:24] PHP Parse error: syntax error, unexpected T_DNUMBER in /home/user/public_html/include/connect.php on line 7
line 7 is my host name line:

Code: Select all

$hostname = 000.000.0.000;
Notice there are no quotes around the IP address. But, when I do put quotes around the IP address, then I get an error message.

I'm really scratching my head over this one..... :?

Thanks in advance - Pavilion

Re: MySql Connection

Posted: Sat Sep 01, 2012 8:50 pm
by Celauran
You need quotes around an IP as it's a string. If your PHP server is on machine A and your SQL server on machine B, you need to make sure your SQL server a.) allows remote connections, and 2.) your user has been granted remote connection privileges (ie GRANT whatever ON database.* TO 'user'@'%' etc)