-1
$conn = new mysqli("localhost", "Voyga");

it works well without password

$conn = new mysqli("localhost", "Voyga", "password");

but it says Access denied for user 'Voyga'@'localhost' (using password: YES)i if I use password.

The password must be right because I can successfully use this password to access mysql in the command line. Why can't I use password if I try to connect mysql in php?

tga
  • 538
  • 4
  • 15
  • could be an issue with the privillages –  Aug 07 '15 at 03:59
  • try to connect it with root and see if its connecting. If yes, there could be some issues with your privileges/configurations ! – Hytool Aug 07 '15 at 04:03
  • can you run this after command-line connect success: `SELECT USER(),CURRENT_USER();` – Drew Aug 07 '15 at 04:26

3 Answers3

0

Check your username and password

Path - (xampp)xampp\phpmyadmin\config.inc.php or (wamp)Wamp\apps\phpmyadmin[phpmyadmin version]\config.inc.php

You can see something like

$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';//username
$cfg['Servers'][$i]['password'] = '';//password
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • I just found that maybe the php is using the mysql installed with MAMP, and in the command line I'm using the stand-alone mysql I installed earlier. And I think that's why I can't access in php, I created a new user in MAMP mysql with phpmyadmin and then it works. How can I access the mysql installed with MAMP in command line? – tga Aug 07 '15 at 13:42
-1

This is example connect to mysql localhost

define('DB_SERVER', 'localhost');

define('DB_USERNAME', 'root');

define('DB_PASSWORD', ''); // set null if you don't use password to connect localhost

define('DB_DATABASE', ''); //your database name

$connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
-1

Might be an issue of privileges. You can try using this instead.

$con=mysql_connect("localhost","username","password");