+ Reply to Thread
Results 1 to 7 of 7

Thread: global mysqli variable?

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    global mysqli variable?

    Trying some simple testing to form the backbone of a subscription system. Why am I getting this error when I make a subscription?
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Mail Subscription</title>
    </head>
    <body>

    <?php
    function DBCon() {
        global 
    $con;
        
    $con mysqli_connect("localhost""user""nopass","mtwinkie_testdb");
        if (
    mysqli_connect_errno()) {
            
    printf("Connect failed: %s\n"mysqli_connect_error());
            exit();
        }
    }

    function 
    Validate($email) {
        if(!
    filter_var($emailFILTER_VALIDATE_EMAIL))
            die(
    "<h1 align=\"center\">Invalid Email.</h1>");
        else if (
    strlen(email) > 150)
            die(
    "<h1 align=\"center\">Email too long.</h1>");
        
    $query "SELECT id FROM Newsletter WHERE email = '" email ."';";
        
    $res mysqli_query($con,$query) or
            die(
    mysqli_error($con));
        if (
    mysqli_num_rows($res) < 1)    
            die(
    "<h1 align=\"center\">Your already subscribed.</h1>");
        return 
    true;
    }

    if (!isset(
    $_REQUEST["do"])) {
    ?>

    <h1>Mail Subscription</h1>
    <form action="#" method="post">
    <b>Your Email Address:</b><br />
    <input type="text" name="email" /><br /><br />
    <b>Action:</b><br />
    <input type="radio" name="do" value="sub" /> Subscribe
    <input type="radio" name="do" value="unsub" /> Unsubscribe
    <p><input type="submit" value="Submit Form" /></p>

    <?php
    } else if ($_REQUEST["do"]=="sub") {
        
    DBCon();
        if (
    Validate($_REQUEST["email"])) {
            
    $query "INSERT INTO Newsletter (ID, Email) VALUES (\"" $email "\");";
            
    $res mysqli_query($con,$query) or
                die(
    mysqli_error($con));
            
    mysql_close($con);
            die(
    "<h1 align=\"center\">Thank you for signing up!</h1>");
        }
    } else if (
    $_REQUEST["do"]=="unsub") {
        die(
    "<h1 align=\"center\">Not available yet =(</h1>");
    } else die(
    "<h1 align=\"center\">Logical Script Error.</h1>");
    ?>

    </body>
    </html>
    As far as I can tell the global declaration should solve this problem?

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: global mysqli variable?

    2 problems:
    Quote Originally Posted by Twinkie View Post
    Code:
    function Validate($email) {
        if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            die("<h1 align=\"center\">Invalid Email.</h1>");
        else if (strlen(email) > 150)
            die("<h1 align=\"center\">Email too long.</h1>");
        $query = "SELECT id FROM Newsletter WHERE email = '" . email ."';";
        $res = mysqli_query($con,$query) or
            die(mysqli_error($con));
        if (mysqli_num_rows($res) < 1)    
            die("<h1 align=\"center\">Your already subscribed.</h1>");
        return true;
    }
    You left the '$' off of 'email' in 2 lines here. Also, grammar maven says that that should be "You're" ("you are"), not "Your" (possessive).

    Quote Originally Posted by Twinkie View Post
    PHP Code:
            $query "INSERT INTO Newsletter (ID, Email) VALUES (\"" $email "\");"
    This is the source of the error. You say you're going to provide 2 values, ID and Email, but only provide 1. If the ID column is auto_increment, leave ID out of the insert statement.

  3. #3
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: global mysqli variable?

    Thanks misson, I already the first two errors after I posted, but that's not what the problem was. I got a warning that I did not have a valid mysqli variable supplied in the email validation function. I later fixed it my moving the mysqli portion of the function where the DBCon() was called, but why can't the script posted above read the $con variable in the Validate() function is $con is a global variable?

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: global mysqli variable?

    Validate() doesn't declare $con as global. I recommend passing $con as a parameter rather than using a global; it's cleaner.

  5. #5
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: global mysqli variable?

    You're probably right, but I declare it as a global variable in the DBCon() function. Shouldn't that make it accessible in the Validate() function?

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: global mysqli variable?

    Quote Originally Posted by Twinkie View Post
    You're probably right, but I declare it as a global variable in the DBCon() function. Shouldn't that make it accessible in the Validate() function?
    Nope. Each function has its own scope. The "global" declaration imports a global variable into a function's scope (actually, it creates a local variable that holds a reference to the global variable). It doesn't make a variable a superglobal.

    In any case, pass the connection around as a parameter and you don't need to worry about globals.
    Last edited by misson; 04-12-2009 at 04:30 PM.

  7. #7
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: global mysqli variable?

    Thanks misson, I can't rep you because I did it before

+ Reply to Thread

Similar Threads

  1. Problema con variable
    By oregonsv in forum Ayuda Web
    Replies: 5
    Last Post: 12-26-2008, 09:37 AM
  2. MySQLi Not working
    By drcore in forum Free Hosting
    Replies: 2
    Last Post: 08-16-2008, 03:23 AM
  3. mysqli not available
    By Eclipse4 in forum Free Hosting
    Replies: 2
    Last Post: 08-13-2008, 12:18 AM
  4. SmartFTP Global Queue
    By TheJeffsta in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 01-17-2008, 02:18 PM
  5. perl script for guestbook, need help
    By shifeng in forum Scripts & 3rd Party Apps
    Replies: 14
    Last Post: 01-06-2008, 12:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers