Skip to main content
Participant
December 15, 2009
Question

Can't connect to Mysql database

  • December 15, 2009
  • 1 reply
  • 847 views

Hello, My teacher has given me acess to his mysql db so I could do a project but I'm having trouble connecting to it.

here is the code that I'm using

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
// we connect to example.com and port 3307
$link = mysql_connect('db****.perfora.net ', 'dbo****** ', 'n****);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>


</body>
</html>

I used *'s so no one could get acess to it besides me

This topic has been closed for replies.

1 reply

Participant
December 15, 2009

Also it gives me this when I try to load the page:


Warning:  mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'db****.perfora.net ' (1) in /Volumes/Storage/webserver/students/2009/web/ftp_01_10/mysql/test1.php on line 12
Could not connect: Unknown MySQL server host 'db2184.perfora.net ' (1)so when I load the page it gives me this :

David_Powers
Inspiring
December 16, 2009

The correct syntax for connecting to a MySQL database is

$link = mysql_connect('hostname', 'username', 'password');

If you're connecting on a nonstandard port, such as 3307 (the default is 3306), you need to add the port number to the hostname, separated by  a colon like this:

$link = mysql_connect('hostname:3307', 'username', 'password');

Most hosting companies do not permit remote connections to a database, so your script needs to be on the same server as the database, unless the correct permissions have been established.

Participant
December 16, 2009

I tried using your code but it still didn't connect I thought I would check the version of php on my teacher's server (not the one that provides him the database, his server in his classroom)

and I used this code


<?php
// This is an example opendb.php
$link = mysql_connect('db****.perfora.net', 'dbo3**26***8 ', 'w**********');

?>