SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Missing ")" in function?

    PHP Code:
    function dbcon($database["host"], $database["user"], $database["pass"], $database["port"])
    {
        
    $connection mysql_connect($database["host"], $database["user"], $database["pass"], $database["port"]);
        if (!
    $connection):
            echo 
    "<div class=\"error\">An error occurred whilst attempting to connect to the database. The error message received from the server was: <i>".mysql_error()."</i>.</div>";
        endif;


    I am sure this is really obvious, but when my script calls this function I get the following error:
    Parse error: parse error, expecting `')'' in /dev/func.php on line 12


    Any ideas?

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks alright to me.. strange.

    try taking out one line at a time to see exactly where the problem is, maybe?

    also, (this is purely opinion): if/endif blocks are evil. Use if () { } syntax. (-:

    S

  3. #3
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't use arrays for function arguments. It should be like this:

    PHP Code:
    function dbcon$host$user$pass$port ) {
        
    $connection mysql_connect$host$user$pass$port );
        if( !isset( 
    $connection ) ) {
            echo 
    '<div class="error">An error occurred whilst attempting to connect to the database. The error message received from the server was: <i>'mysql_error(), '</i>.</div>';
        }
        return 
    connection;

    ck :: bringing chris to the masses.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, okay. Thanks Anarchos.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // Begin Configuration
    $database_host "localhost"// Can normally be left as is, consult your host if errors occur.
    $database_user "root";      // You will probably have to change this, it is most likely the same as your FTP username.
    $database_pass "";          // You will probably have to change this, it is most likely the same as your FTP password.
    $database_port "3306";      // Can almost always be left as is.
    // End Configuration

    function connect2db($database_host$database_user$database_pass$database_port)
    {    
        
    $connection mysql_connect($database_host$database_user$database_pass$database_port);
        if (!
    $connection) {
            echo 
    "<div class=\"error\">An error occurred whilst attempting to connect to the database. The error message received from the server was: <i>".mysql_error()."</i>.</div>";
        }
        return 
    $connection;

    Now, when I call connect2db(); it won't use the values I have configured in the configuration section of the script -- how come? Is there no way I can just call connect2db();?
    Last edited by Bob; Feb 12, 2002 at 14:34.

  6. #6
    SitePoint Enthusiast numeropi's Avatar
    Join Date
    Apr 2001
    Location
    somewhere between 3.14 and 3.15
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you have two choices:

    a) give default parameter values so that these are used when you don't explicitly set them, i.e.:

    Code:
    function connect2db($database_host="localhost", $database_user="root", $database_pass="", $database_port="3306");
    b) create the function without parameters, then import the global variables you declared before

    Code:
    function connect2db()
    {
        global $database_host;
        global $database_user;
        global $database_pass;
        global $database_port;
    }
    Both ways, it will work if you just call connect2db();
    No más enlaces rotos en el correo: EnlaceBreve.com
    Tu bitácora en castellano: BitacoraFacil.com

  7. #7
    SitePoint Addict
    Join Date
    Nov 2001
    Location
    Dublin, Ireland
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using the second method, thanks a lot!

  8. #8
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm dumb. I can't believe I said it looked ok to me.. sorry about that. Slipped right through (-:

    As for Anarchos' re-arranging of the 'if' block, $connection will always be set. It SHOULD read:

    PHP Code:
    if (!$connection) {
            echo 
    '<div class="error">An error occurred whilst attempting to connect to the database. The error message received from the server was: <i>'mysql_error(), '</i>.</div>';
        } 
    To check if $connection is FALSE, not set.

    S

  9. #9
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops you're right, I wasn't paying attention
    ck :: bringing chris to the masses.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •