Log in

View Full Version : mysql_connect works, but mysqli_connect doesn't...



ShootingBlanks
09-28-2007, 07:29 PM
When I use this code:
$link = mysql_connect($hostname_SalesRepository, '*user*', $password_SalesRepository, 'salesrepository');
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

printf("Host information: %s\n", mysql_get_host_info($link));

Everything works and I get the following message:

Host information: Localhost via UNIX socket

But when I use THIS code:
$link = mysqli_connect($hostname_SalesRepository, '*user*', $password_SalesRepository, 'salesrepository');
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

printf("Host information: %s\n", mysqli_get_host_info($link));

Nothing works, and I get THIS message:

Connect failed: Access denied for user '*user*'@'localhost' to database 'salesrepository'

Can anyone help me out with this??? Thanks!...


MOD EDIT: Replaced username with *user* for security. Two pieces to the puzzle are better for stopping a potential hacker. Don't give out the username either.

djr33
09-28-2007, 09:39 PM
Nothing jumps out at me.

First, are you sure you have mysqli included with PHP and your MySQL db is 4.1.3 or higher?
Requirements
In order to have these functions available, you must compile PHP with support for the mysqli extension.
Note: The mysqli extension is designed to work with the version 4.1.3 or above of MySQL.
<?php echo function_exists('mysqli_connect')?'yes':'no'; ?>
That will output yes/no if the function mysqli_connect exists in your PHP configuration.

And a few more things--
Note that in the first block of code you are using mysqli_connect_error() rather than as I suspect you'd want to mysql_connect_error(). That isn't the reason it's not working, though.

I don't understand why you're using printf() for that. Just using echo 'message: '.function()."\n"; would be much easier. Again, not the problem.

I doubt this is really the reason it isn't working, but it's a good idea to check if there is a value of a function, not if it's value equates to true due to type conversion.
if (!link) should be if (!isset($link)) and it will also not give an error running running the E_ALL error mode if it isn't set. You could also use empty() instead of !isset().
If you're lucky, that might actually fix it, but unlikely.

ShootingBlanks
09-28-2007, 09:42 PM
I actually realized that it was a careless error on my part. I was typing the wrong database name. I still don't understand why there was no error generated with the mysql_connect, though. But oh well - I guess it doesn't matter if everything is working now!...

...thanks!!!

djr33
09-28-2007, 09:45 PM
Ah, ok. I just added some more info by editing the post. It explains why there was no error.

ShootingBlanks
09-28-2007, 09:50 PM
Awesome - thanks for all the good info. Much appreciated! :)