0

I'm using Mac OSX and I have MAMP installed, which is running my localhost server. However, there are some issues running the PHP code because the website keeps returning that there is an error when connecting to the server. I don't know what's wrong with this; maybe someone can help me out here.

PHP code:

<?php

$connection = mysql_connect("localhost", "user", "password") or die ("There was an error when connecting to the server");
mysql_select_db("topaz", $connection) or die ("There was an error when connecting to the database");

echo "

  <body style='font-family: Helvetica Neue, Helvetica, Arial, sans-serif;'>

    <div style='width: 80%; padding: 10px; border: 1px solid #000000; background-color: #ffffff;'>
      <h1>Login</h1>
    </div>

  </body>
";

?>

PHPMyAdmin Settings: enter image description here

MAMP Port Settings: enter image description here

Error: enter image description here

2 Answers 2

1

You need to establish the connection over the port specified in your control panel - by default, mysql_connect() will attempt to connect to your MySQL host over port 3306.

Specify which port to use in the first parameter (host) of mysql_connect:

$connection = mysql_connect("localhost:8889", "user", "password") or die ("There was an error when connecting to the server");
//                               Here ^^^^^

Mandatory note: Don't use mysql_* functions as they're deprecated. Use PDO or mysqli_* instead.

Sign up to request clarification or add additional context in comments.

2 Comments

@Shrey Have you tried establishing a connection over the default MySQL port (3306)?
I'm afraid I can't help any further then, have you tried the solutions listed here: forum.mamp.info/viewtopic.php?t=115 ?
0
<?php
/*
link.php
Created By Nicholas English
*/
$link = null;
$connection = null;
$servername = "";
$username = "";
$dbname = "";
$pass = "";
$mysqli = null;
$pdo = null;
$obj = null;
$pr = null;
$type = 3;
if ($type === 1) {
$mysqli = true;
$pdo = false;
$obj = true;
$pr = false;
} else {
if ($type === 2) {
$mysqli = true;
$pdo = false;
$obj = false;
$pr = true;
} else {
if ($type === 3) {
$mysqli = false;
$pdo = true;
$obj = false;
$pr = false;
} else {
$mysqli = null;
$pdo = null;
$obj = null;
$pr = null;
}
}
}
if ($mysqli === true && $obj === true) {
$link = new mysqli($servername, $username, $pass, $dbname);
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$connection = true;
} else {
if ($mysqli === true && $pr === true) {
$link = mysqli_connect($servername, $username, $pass, $dbname);
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$connection = true;
} else {
if ($pdo === true && $mysqli === false) {
try {
$link = new PDO("mysql:host=$servername;dbname=$dbname", $username, $pass);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection = true;
}
catch(PDOException $e)
{
$connection = null;
echo "Connection failed: " . $e->getMessage();
}
} else {
$link = null;
$connection = null;
}
}
}
if ($connection == null && $link == null) {
$error = 1;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.