views:

53

answers:

3

I've just made a database on mysql on my server. I want to connect to this via my website using php. This is the contents of my connections file:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Error connecting to mysql');

$dbname = 'epub';
mysql_select_db($dbname);

I know what the username/passwords are, and I know the IP address of the server. What I'm just wondering is how do I know which port to use?

+3  A: 

If your MySQL server runs on default settings, you don't need to specify that.

Default MySQL port is 3306.

[updated to show mysql_error() usage]

$conn = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Error connecting to mysql: '.mysql_error());
Mchl
I've tried both the IP address on it's own, and with :3306 appended to the end of it, none work. Is there a way I can find out if there is a different port? I did not set up this server.
TaraWalsh
Fitst you should check what is the error you're getting from mysql_connect(). Use mysql_error() function for that. Knowing what the error is, will help us find out what is going on, and what's the actual issue.
Mchl
@Mchl The error is: Can't connect to MySQL server on 'xxx.xx.xx.xx' (10060)
TaraWalsh
Never mind. I added a rule in the firewall settings for the port number. I thought this had been done before hand. Thanks anyway
TaraWalsh
+1  A: 

If you specify 'localhost' the client libs default to using the filesystem system socket on a Unix system - trying the mysql_default_socket value from php.ini (if set) then the my.cnf value.

If you connect using a different tool, try issuing the command "show variables like '%socket%'"

If you want to use a network port (which is a wee bit slower) then try specifying 127.0.0.1 or a physical interface asociated with the machine.

symcbean
A: 

check this out dude

<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
adisembiring