php: database connection function

MySql database connection looks like this


and it appears in the code many times – and in different sources. I thought of writing a ConnectDB($DB_name) function and placing it in funcs.php and including it in the necessary source code (include “funcs.php”)

When I call my function, I get an error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO)

If you connect not through my self-written function, then it connects normally.
The question is why does this error occur? So how do you write such a function?

By the way, even if the function is declared at the top of the source, the same error still occurs.
Those. unable to connect via function.

Answer 1, authority 100%

If you created a separate document for connecting to the database, then why else pack it into a function? Well, to create a class – I still understand, otherwise write the connection in the usual way and connect the document where necessary.

Leave a reply

Please enter your comment!
Please enter your name here