mysql_error function
am 03.10.2006 10:29:10 von Honza Spurny
Hello, let me ask you something. I have a problem with mysql_error()
function after using mysql_connect(). Please look at thiese two examples:
$link = mysql_connect($host, $user, $pswd) or die("Could not connect to
$host because: " . mysql_error());
$link1 = mysql_connect($host1, $user, $pswd) or die("Could not connect to
$host because: " . mysql_error());
mysql_select_db($db, $link1);
// ... and here the code is going
// to query the selected DB
// Then I need to connect to another DB
$link2 = mysql_connect($host2, $user, $pswd) or die("Could not connect to
$host because: " . mysql_error());
// ... and here the code is going to work
// with another connection to another database
QUESTION and PROBLEM:
The first example work excelent, so when for example bad password is given,
the output is: "Could not connect do _HOST_ because: Access denied for user
'_USER_'@'%'"
(or something like it)
Lets have a look on the second example:
Lets say, that first connection works perfectly, so all quiries are well
done. But lets say, that second connection has wrong user/password given.
Then the error output is:
"Could not connect to _HOST2_ because:"
The error message for second connection request is not given back to the
PHP. Or as I suppose, mysql_error takes last error message from last used
ESTABLISHED connection (it means from $link1) and since in this connection
was everything ok, it puts empty string as a result of mysql_error function.
What can I do to get really last error message from mysql server? Last error
message is dedicated to last mysql_connection function, but since $link2 is
not established it is not given as a result of mysql_error function.
Can anybody help me, how to get "really last" error message that is
dedicated to last connection request?
Thanks a lot for any advice.
Honza Spurny
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mysql_error function
am 03.10.2006 19:28:34 von Niel Archer
Hi
> The error message for second connection request is not given back to the
> PHP. Or as I suppose, mysql_error takes last error message from last used
> ESTABLISHED connection (it means from $link1) and since in this connection
> was everything ok, it puts empty string as a result of mysql_error function.
This is incorrect. mysql_error() retreives the error message from the
last mysql function (excluding itself or mysql_errno) executed, not the
last connection.
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mysql_error function
am 05.10.2006 11:33:35 von Honza Spurny
Niel Archer wrote:
> Hi
>
>> The error message for second connection request is not given back to
>> the PHP. Or as I suppose, mysql_error takes last error message from
>> last used ESTABLISHED connection (it means from $link1) and since in
>> this connection was everything ok, it puts empty string as a result
>> of mysql_error function.
>
> This is incorrect. mysql_error() retreives the error message from the
> last mysql function (excluding itself or mysql_errno) executed, not
> the last connection.
>
OK, so there is no way how to get error message about mysql_connect()
function when establishing more connections?
The strange thing is, that it works fine if You use just one connection. In
this case, mysql_error() function can takes error message from the
mysql_connect() function...
>
> Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mysql_error function
am 05.10.2006 15:53:47 von Niel Archer
Hi
> OK, so there is no way how to get error message about mysql_connect()
> function when establishing more connections?
mysql_error *should* hold the error string from the last used mysql
function (excepting itself and mysql_errno), that includes the
mysql_connect() function as it's a mysql function.
> The strange thing is, that it works fine if You use just one connection. In
> this case, mysql_error() function can takes error message from the
> mysql_connect() function...
However, if you don't supply a resource idintifier it will asume the last
opened link, hence your problem on the second connection failure. When
using more than one link, you should specify the link resource for
mysql_error(), which is difficult for the mysql_connect() function,
obviously. I'm not sure if using the returned FALSE value would work,
as I've never had a problem with additional connections.
All of this is clearly decribed on the manual page.
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mysql_error function
am 06.10.2006 13:26:06 von Honza Spurny
Niel Archer wrote:
> Hi
>
>> OK, so there is no way how to get error message about mysql_connect()
>> function when establishing more connections?
>
> mysql_error *should* hold the error string from the last used mysql
> function (excepting itself and mysql_errno), that includes the
> mysql_connect() function as it's a mysql function.
>
>> The strange thing is, that it works fine if You use just one
>> connection. In this case, mysql_error() function can takes error
>> message from the mysql_connect() function...
>
> However, if you don't supply a resource idintifier it will asume the
> last opened link, hence your problem on the second connection
> failure. When using more than one link, you should specify the link
> resource for mysql_error(), which is difficult for the
> mysql_connect() function, obviously. I'm not sure if using the
> returned FALSE value would work,
> as I've never had a problem with additional connections.
>
YES, this is the problem.
Since
$dblink2 = mysql_connect(........) is getting false (so no resource object),
so $dblink is not correct parameter for mysql_error() function and it works
same way as there is no parameter.
OK, so mysql_error is not usable for me in this case.
Is in PHP any other way how to get error-message from connect function?
Isn't in PHP any other function, that can be used similiar?
> All of this is clearly decribed on the manual page.
>
> Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mysql_error function
am 06.10.2006 16:06:19 von Niel Archer
Hi
> OK, so mysql_error is not usable for me in this case.
> Is in PHP any other way how to get error-message from connect function?
> Isn't in PHP any other function, that can be used similiar?
To the best of my knowledge, there is no work around for this situation.
Even the 'Improved' MySQL Extension suffers this same limitation, so no
improvement there.
You can of course still determine failure, but not directly the cause.
The only ways I can think of to begin identifying the error would be to
look up supplied username/password with the MySQL permissions db to
verify their authenticity, or to close the first connection and retry
the second after so it is the only connection.
Niel
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php