Search code examples
phpmysqlmamp

PHP/MySQL newbie Got Packets out of order


I'm trying to follow this tutorial http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app

I'm about half way down the page Verifying PHP/MySQL functionality and am stuck.

I just set up MAMP for the first time. I have two files in my MAMP htdocs directory

index.php

<?php
    echo "Hello, PHP!";
?>

promocodes.php

<?php

class RedeemAPI {
    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('localhost', 'root', 'root', 'promos');
        $this->db->autocommit(FALSE);
    }

    // Destructor - close DB connection
    function __destruct() {
        $this->db->close();
    }

    // Main method to redeem a code
    function redeem() {
        // Print all codes in database
        echo "Hello, PHP!";
        $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
        echo "Hello, PHP!";
        $stmt->execute();
        $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
        while ($stmt->fetch()) {
            echo "$code has $uses_remaining uses remaining!";
        }
        $stmt->close();
    }
}

?>

when I go to http://localhost:8888/index.php I get

"Hello, PHP!";

when I go to http://localhost:8889/index.php I get the following gibberish

H��� 5.5.9�G���}Se"tHIr�ÿ÷�€����������(ja&7"BfZoB<�mysql_native_password!��ÿ„#08S01Got packets out of order

when I go to http://localhost:8888/promocodes.php I get

 

when I go to http://localhost:8889/promocodes.php I get the following gibberish

H��� 5.5.9�H���x%J'"s@#�ÿ÷�€����������-YX0#_89T|50�mysql_native_password!��ÿ„#08S01Got packets out of order


Solution

  • Found the problem, when I copied in the new RedeemAPI class I deleted the code that actually uses the class.

    // This is the first thing that gets called when this page is loaded
    // Creates a new instance of the RedeemAPI class and calls the redeem method
    $api = new RedeemAPI;
    $api->redeem();