PHP loses contact with MYSQL

Discussion in 'Web Design and Development' started by Starhorsepax, Apr 23, 2011.

  1. Starhorsepax, Apr 23, 2011
    Last edited by a moderator: Apr 26, 2011

    Starhorsepax macrumors member

    Starhorsepax

    Joined:
    Jun 6, 2010
    #1
    Hi.

    I'm learning PHP and got this code from Sam's teach yourself PHP, MYSQL and APACHE. I can connect to the MYSQL server fine from terminal or PHPMyAdmin. The first part of this e-mail form works fine. It will subscribe and add the emails to the database. But it won't delete them, in fact it doesn't even seem to know they are there.

    I get the impression it's losing it's connection to the database for some reason. Does anyone know if this could be something misconfigured somewhere?

    Here is the code: (I took out the password/user info.) The part with the confused smiley is where it breaks down.

    PHP:
    <?php

    function doDB() {
        global 
    $mysqli;
        
        
    //connect to server and select database; you may need it
        
    $mysqli mysqli_connect("localhost""user""password""newsletter");
        
        
    //if connection fails, stop script execution
        
    if (mysqli_connect_errno()) {
            
    printf("Connect failed: %s\n"mysqli_connect_error());
            exit();
            }
        }
        
        function 
    emailChecker($email) {
            global 
    $mysqli$check_res;
            
            
    //check that email is not already in list
            
            
    $check_sql "SELECT id FROM SUBSCRIBERS
            WHERE email = ' "
    .$email."'";
            
    $check_res mysqli_query($mysqli$check_sql)
                or die(
    mysqli_error($mysqli));
                }

    ?>

    <?php

    include("ch19_include.php");

    //determine if they need to see the form or not
    if (!$_POST) {
        
    //they need to see the form, so create form block
        
    $display_block "
        <form method=\"POST\" action=\""
    .$_SERVER["PHP_SELF"]."\">
        
        <p><strong>Your E-Mail Address:</strong><br/>
        <input type =\"text\" name=\"email\" size=\"40\">
        
        <p><strong>Action:</strong><br/>
        <input type=\"radio\" name=\"action\"
            value=\"sub\" checked> subscribe
        <input type=\"radio\" name=\"action\"
            value=\"unsub\" > unsubscribe
            
            <p><input type=\"submit\" name=\"submit\"
                value=\"Submit Form\"</p>
                </form>"
    ;
                
    }  else if ((
    $_POST) && ($_POST["action"] == "sub")) {
        
    //trying to subscribe; validate email address
        
    if ($_POST["email"] == "") {
            
    header("Location: subscriberform.php");
            exit;
            } else {
            
    //connect to database
            
    doDB();
            
            
    //check that email is in list 
            
    emailChecker($_POST["email"]);
            
            
    //get number of results and do action
            
    if (mysqli_num_rows($check_res) < 1) {
            
    //free result
            
    mysqli_free_result($check_res);
            
            
    //add record
            
    $add_sql "INSERT INTO Subscribers(email) VALUES('".$_POST["email"]."')";
            
    $add_res mysqli_query($mysqli$add_sql) or die(mysqli_error($mysqli));
            
    $display_block "<p>Thanks for signing up!</p>";
            
            
    //close connection to MySQL
            
    mysqli_close($mysqli);
            } else {
            
    //print failure message
            
    $display_block "<p>You're already subscribed!</p>";
            }
        }
        
    }  else if ((
    $_POST) && ($_POST["action"] == "unsub")) {
    //trying to unsubscribe; validate email adress
        
    if ($_POST["email"] == "") {
            
    header("Location: subscriberform.php");
            
    //exit;
            
    } else {
            
            
            :
    confused://connect to database :confused:
            
    doDB();
            
            
    //check that email is in list
            
    emailChecker($_POST["email"]);
            
            
    //get number of results and do action altered from < 1 to < 0 
            
    if (mysqli_num_rows($check_res) < 0) {
            
    //free result
            
    mysqli_free_result($check_res);
            
    //print failure message
            
    $display_block "<p>Couldn't find your address!</p>
            <p>No action was taken.</p>"
    ;
            } else {
            
            
    //get value of ID from result
            

      
    $result mysql_query("SELECT id, name FROM Subscribers");
            
            while (
    $row mysql_fetch_array($ResultMYSQL_ASSOC)) {
        
    printf ($result);
        
        
    //$id = $row["id"];
            
            
    }
            
    //$res = mysql_query($sql) or exit( mysql_error() );
            //unsubscribe the address
            
    $del_sql "DELETE FROM Subscribers WHERE ('".$_POST["email"]."')";/*id = '".$id."'";*/
            
    $del_res mysqli_query($mysqli$del_sql) or die(mysqli_error($mysqli));
            
    $display_block "<p>You're unsubscribed!</p>";
                        }
                        
    mysqli_close($mysqli);
                        }
                    }
                    
                    
    ?>
                    
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <title>Subscribe/Unsubscribe</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>Subscribe/Unsubscribe to Mailing List</h1>
    <?php echo "$display_block"?>
    </body>
    </html>
     
  2. wpotere Guest

    Joined:
    Oct 7, 2010
    #2
    Does the user account that you are using in the script have the permissions to actually delete items from the database? The code looks correct and doDB(); calls the connection method so the only thing I can think of is permissions.
     
  3. Starhorsepax thread starter macrumors member

    Starhorsepax

    Joined:
    Jun 6, 2010
    #3
    I'm using root. The same account works fine from PHPMyAdmin and in Terminal to both add and delete.

    :confused:I did notice one thing odd: when I first installed MySql, various internet sources said the mysql sock was not in the right place. I found it in: /private/tmp/mysql.sock now however I'm noticing there is also one in /tmp/mysql.sock that wasn't there before. I am suspecting something might be misconfigured in php.ini or something.
     
  4. wpotere Guest

    Joined:
    Oct 7, 2010
    #4
    Might be worth removing MySQL all together and re-installing it. The documentation that I used is this...

    http://dev.mysql.com/doc/refman/5.0/en/macosx-installation.html

    Then make sure that the PHP.ini has the proper information on the location. To be honest, it was easier to use this.

    http://www.mamp.info/en/index.html
     
  5. Starhorsepax thread starter macrumors member

    Starhorsepax

    Joined:
    Jun 6, 2010
    #5
    I've just noticed something else in the console firewall log:

    Firewall[42]: Deny nmblookup data in from 192.168.1.1:137 uid = 0 proto=17

    According to the web sharing in system preferances I'm using 192.168.1.1
    Is it possible I'm being locked out by the firewall all of a sudden?
     
  6. wpotere Guest

    Joined:
    Oct 7, 2010
    #6
    One way to find out is to turn off the firewall temporarily and give it a try. Just make sure you are connected to a network that is not open to the world (running a NAT or behind its own firewall). It is possible that the local firewall software might be stopping you.
     
  7. Starhorsepax thread starter macrumors member

    Starhorsepax

    Joined:
    Jun 6, 2010
    #7
    Didn't work.

    I've gone ahead and done all the other exercises in the book. Oddly, this is the only one with a problem reconnecting to the database.
     
  8. wpotere Guest

    Joined:
    Oct 7, 2010
    #8
    Weird... Something you might try is to actually redo the entire connection string instead of calling the function. Not sure why that wouldn't work but there are all sorts of variables that could cause the connection to drop.

    So instead of doDB()

    use

    $mysqli = mysqli_connect("localhost", "user", "password", "newsletter");

    //if connection fails, stop script execution
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

    You may need to play with it a bit (PHP is not my strong suite) but that would be my next step on that one.
     

Share This Page