php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21209 Error in the examples for mysql_fetch_assoc and mysql_fetch_array
Submitted: 2002-12-26 18:51 UTC Modified: 2003-01-04 14:13 UTC
From: kyrael at web dot de Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 4CVS-2002-12-26 (dev) OS: Windows
Private report: No CVE-ID:
 [2002-12-26 18:51 UTC] kyrael at web dot de
Hi!

There is an error in the code examples for mysql_fetch_assoc and _array:
(for mysql_fetch_assoc, at the page for _array is the same error)
[code]
    $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
    
    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }
[/code]

That doesn't make sense. mysql_error() takes the connection that is passed as an argument or the last opened connection. Where mysql_error() is called, no connection to a mysql server is established, so mysql_error() returns an empty string. Additionaly PHP raises an E_WARNING error anyway in case mysql_connect fails. Sample Output: (custom error handler)

[output]
Warning:
mysql_connect() [function.mysql-connect]: Access denied for user: 'mysql_user@localhost' (Using password: YES)
On Line: 2
In File: c:\web\apache\htdocs\test.php
Error Context: $conn = mysql_connect("localhost", "mysql_user", "mysql_password");

Unable to connect to DB:
[/output]

Suggestion:
a) Change the examples so that they catch the errors in a way that is appropriate, i.e.:
[code]
$conn = @mysql_connect("localhost", "mysql_user", "mysql_password");

    if (empty($conn)) {
        echo "Unable to connect to DB: " . $GLOBALS['php_errormsg'];
        exit;
    }
[/code]
b) More work, but would be nicer and match the documentation for mysql_error - yet this changes the behaviour a lot, some scripts would have to be rewritten:
Let mysql_connect no longer issue warnings ("Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_error() to retrieve the error text." - Manual page for mysql_error() ), but modify mysql_error so that it holds error strings from mysql_connect as well.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-03 17:25 UTC] nicos@php.net
Actually with 

<?php
    @mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
?>

It returns:

Could not connect: Access denied for user: 'mysql_user@localhost' (Using password: YES)

so using mysql_error() here is just fine, it also manages the connections' error.

Thank you for your report.
 [2003-01-03 20:37 UTC] kyrael at web dot de
That is not true.
Your code just produces a ``Could not connect: ''
here.

PHP Version 4.4.0-dev
System Windows NT localhost 5.1 build 2600
Build Date Dec 26 2002 20:10:08
Server API Apache
 [2003-01-03 23:54 UTC] michael dot mauch at gmx dot de
The manual states that mysql_error() without arguments retrieves the error text of the last recently used MySQL function - _not_ the last opened connection.

And that's exactly what happens at least here on Linux (tested with 4.3.0 and 4.4.0-CVS as of today).

So if the example doesn't work on Windows, it is broken on Windows. Can you try the example on its own, without your custom error handler?
 [2003-01-04 05:23 UTC] nicos@php.net
Are you using the bundled version of MySQL, if not, which version?

I can't reproduce the bug in any of my systems.

Anyway changing this to a MySQL bug, and will see if it is verified.
 [2003-01-04 08:00 UTC] goba@php.net
Michael note that the function states that it gives the error of the last myqsl operation, but the note says on the page:

| Note: If the optional argument is specified the
| given link is used to retrieve the error message.
| If not, the last opened link is used. 

So it implies, that if the parameter is not given,
an opened connection is needed... So the documentation
also needs to be changed.

Please do not close this bug before the mysql_error
documentation is fixed.
 [2003-01-04 12:23 UTC] georg@php.net
Unless you use an outdated libmysql, or mysql wasn't installed property or a valid (default connection) already exists, mysql_error() returns an errormessage, even if the connection failed.
 [2003-01-04 14:13 UTC] kyrael at web dot de
>Unless you use an outdated libmysql, or mysql wasn't installed property
>or a valid (default connection) already exists, mysql_error() returns an
>errormessage, even if the connection failed.

Ah, thats the problem. A MySQL connection is established in the prepend file.

Still, that behaviour doesn't make not much sense to me. (Only return errors if no connections are already established)

And the manual clearly states that mysql_error uses only open connections, so that should be fixed as well.
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Sat Mar 28 05:03:12 2015 UTC