/    Sign up×
Community /Pin to ProfileBookmark

Error connecting to MYSQL with MYSQLi_Connect … mysql_connect works!

Hi All,

I am trying to connect to a hosted MYSQL database. If I use MYSQL connection code …

[code=php]DEFINE (‘DB_USER’, ‘ff_admin’);
DEFINE (‘DB_PASSWORD’, ‘xxxxx’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘ff_data’);

$dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error());
mysql_select_db(DB_NAME) or die (mysql_error());[/code]

The connection is successful. If i use the following MYSQLI code … the connection fails

[code=php]$servername = “localhost”;
$username = “ff_admin”;
$password = “xxxxx”;
$database = “ff_data”;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}
echo “Connected successfully”;[/code]

The error message shown is …”Connection failed: Access denied for user ‘ff_admin’@’localhost’ (using password: YES)”

I have checked for mysqli_connect support using the below code …

[code=php]echo function_exists(“mysqli_connect”); // outputs 1 for true 0 for false[/code]

And obtained a 1. Any ideas why the MYSQL connection works and the MYSQLi doesn’t using the same authentication details?

Many thanks for any help offered.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 11.2015 — Can I assume you've made absolutely sure the password is exactly the same in each case (including case, white-space, etc.)?
Copy linkTweet thisAlerts:
@NogDogJun 11.2015 — Also, I think there's a minimum version of MySQL that will work with the MySQLi extension, so you might want to check that from your site config info. (Looks like it needs at least 4.3.1.)
Copy linkTweet thisAlerts:
@carpauthorJun 11.2015 — Hi NogDog,

Thank you for your reply ... any assistance is most appreciated.

I have run phpinfo() on the server and determined that the version of php running is 5.3.19

In addition I can confirm that all of the authentication details in the two connection files are exactly the same!

Any ideas?

Many thanks
Copy linkTweet thisAlerts:
@NogDogJun 11.2015 — The version of interest in this case is the actual MySQL DB installation -- should be visible in the PHPMyAdmin interface if you use that. (Not sure if it shows up in the phpinfo() output?) I believe 4.3.1 is pretty old now, so I would hope you're on 5.x by now, but you never know.
Copy linkTweet thisAlerts:
@carpauthorJun 11.2015 — Hi NogDog,

This is the info I was able to determine from PHPMyAdmin ...

mysql

MySQL Support enabled

Active Persistent Links 0

Active Links 0

Client API version 5.5.32

MYSQL_MODULE_TYPE external

MYSQL_SOCKET /var/lib/mysql/mysql.sock

MYSQL_INCLUDE -I/usr/include/mysql

MYSQL_LIBS -L/usr/lib64 -lmysqlclient

Directive Local Value Master Value

mysql.allow_local_infile On On

mysql.allow_persistent On On

mysql.connect_timeout 60 60

mysql.default_host no value no value

mysql.default_password no value no value

mysql.default_port no value no value

mysql.default_socket /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock

mysql.default_user no value no value

mysql.max_links Unlimited Unlimited

mysql.max_persistent Unlimited Unlimited

mysql.trace_mode Off Off

mysqli

MysqlI Support enabled

Client API library version 5.5.32

Active Persistent Links 0

Inactive Persistent Links 0

Active Links 0

Client API header version 5.5.27

MYSQLI_SOCKET /var/lib/mysql/mysql.sock

Directive Local Value Master Value

mysqli.allow_local_infile On On

mysqli.allow_persistent On On

mysqli.default_host no value no value

mysqli.default_port 3306 3306

mysqli.default_pw no value no value

mysqli.default_socket /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock

mysqli.default_user no value no value

mysqli.max_links Unlimited Unlimited

mysqli.max_persistent Unlimited Unlimited

mysqli.reconnect Off Off

Hope this helps, many thanks
Copy linkTweet thisAlerts:
@NogDogJun 11.2015 — Unfortunately I'm on PostgreSQL here at work, so don't have phpMyAdmin to look at. I'm not sure if those version numbers have anything to do with the actual DB version, or just the PHP API being used. However, it looks like we could make sure with a simple script like this:
[code=php]
<?php
$db = mysql_connect(<your connection values here as normal>);
$result = mysql_query("SHOW VARIABLES LIKE 'version'");
echo "<pre>".print_r(mysql_fetch_assoc($result), true)."</pre>";
[/code]

(untested, so who knows what stupid mistakes I've made ? )
Copy linkTweet thisAlerts:
@rootJun 13.2015 — Says...
mysqli

MysqlI Support enabled[/QUOTE]


So mysqli is supported.

I would have taken a more OO approach to the DB side of things, for example...

[code=php]
$servername = "localhost";
$username = "ff_admin";
$password = "xxxxx";
$database = "ff_data";

// Create connection
$db = new mysqli( $servername, $username, $password, $database );

// Check connection
if( $db->connect_errno )
die("Connection failed: " . $db->connect_error );

echo "Connected successfully";

[/code]
×

Success!

Help @carp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...