Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Can't connect to Mysql database

New Here ,
Dec 15, 2009 Dec 15, 2009

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

TOPICS
Server side applications
855
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2009 Dec 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 :

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 16, 2009 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 16, 2009 Dec 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)

Picture 1.png

and I used this code


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

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 16, 2009 Dec 16, 2009
LATEST

The code that I gave you was generic code. You replace 'hostname', 'username', and 'password' with the actual values. Judging from the code you have posted here, that's what you have done. Use die to get any error message:

$link = mysql_connect('db****.perfora.net', 'dbo3**26***8 ', 'w**********')
  or die(mysql_error());

I suspect that you are trying a remote connection, which is being rejected by MySQL. If that's the case, you'll need to get your teacher to change the permissions to allow remote connections.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines