polin
polin

Reputation: 2845

php database connection error for changing port number

I am facing database connection problem while trying to connect a .php file through wamp server
the error message is something like- "Access denied for the user " @ ' localhost' " for database
'aschool'. 'aschool' is my database name.
Mentioning that I've changed my port number of wamp server, I am worried that is it really for changing port number or anything else.Here is my code.

$con = mysql_connect();
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aschool", $con);

After this line the error message comes. I've tried parameters "localhost" inside the mysql_connect()
function or more parameters but the result is same.
Thanks in advance anyone gives me any solution

Upvotes: 0

Views: 947

Answers (1)

Benjamin Diele
Benjamin Diele

Reputation: 1187

That's because you are using mysql_connectwrong for your use case.

If you check the documentation page it says that you can also a server-path, something like mysql_connect('localhost:1234', 'username', 'password').

But you shouldn't use mysql_connect.

Use PDO so that you can use parameterized queries.

In code it would go like this:

try
{
  $pdo = new \PDO('mysql:dbname=aschool;host=127.0.0.1', 'myUser', 'myPassword');
} catch (PDOException $exception)
{
  // Do something with your exception.
  // Echo it, dump it, log it, die it.
  // Just don't ignore the exceptions!
}

Upvotes: 2

Related Questions