From:             [EMAIL PROTECTED]
Operating system: Windows
PHP version:      4CVS-2002-12-26 (dev)
PHP Bug Type:     Documentation problem
Bug description:  Error in the examples for mysql_fetch_assoc and mysql_fetch_array

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.


-- 
Edit bug report at http://bugs.php.net/?id=21209&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=21209&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=21209&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=21209&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=21209&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=21209&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=21209&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=21209&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=21209&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=21209&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=21209&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21209&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=21209&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=21209&r=isapi


-- 
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to