• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Mysql connection with php


I have setup a mysql server to test a web application. using version 5.5.11, I have a php web site that I am using to access it but it always gives me the error cant connect to db.

I am able to connect with workbench from my laptop (remote) and my phone with no problems but the web site always returns

cannot connect to MySQL server on xx.xx.xx.xx (4)

Do I need to specify the port in the connnection?


0
JonMny
Asked:
JonMny
  • 2
  • 2
1 Solution
 
brutaldevCommented:
Try this code in just a plain PHP page to test the connection.
 
<?php
  mysql_connect('IP address or host name', 'username', 'password') or die("Unable to connect to database");
  mysql_select_db('database name') or die( "Unable to select database");
  mysql_close();
?>

Open in new window

Fill in the same details that you can see when connecting the the database using workbench.
0
 
brutaldevCommented:
3306 is the default port so if that's what you're connecting with (you can see in workbench) then you don't need to specify the port. You can specify the port in the format ipaddress:port as you can see in workbench if you need to.

More info here: http://php.net/manual/en/function.mysql-connect.php
0
 
JonMnyAuthor Commented:
I user the sample code and now it's giving me

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'test'@'192.168.1.1' (using password: YES) in Test.php on line 2
Unable to connect to database
0
 
Ray PaseurCommented:
That is actually better than the first message because you are now talking to MySQL.  Set up the account and password and then connect something like this code example.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

Open in new window

0
 
JonMnyAuthor Commented:
Thanks adding :3306 fixed the problem the access denied error was an issue with the web host I was using.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now