0

按照在线教程并设法获得此代码:

class RedeemAPI {
private $db;

// Constructor - open DB connection
function __construct() {
    $this->db = new mysqli('localhost', 'username', 'password', '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
    $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
    $stmt->execute();
    $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
    while ($stmt->fetch()) {
        echo "$code has $uses_remaining uses remaining!";
    }
    $stmt->close();
}
}

紧接着,那家伙说我应该运行该页面并看到以下内容:

 test has 10000 uses remaining!

但什么也没发生,页面只是空白。数据在数据库中,并且凭据是 100% 正确的。

有什么想法吗?谢谢。

4

4 回答 4

2

您的代码应该包括:$api = new RedeemAPI();$api->redeem();,仅仅因为本教程不包括进一步下降并不意味着您不使用它。

所以试试:

error_reporting(E_ALL);
class RedeemAPI {
private $db;

// Constructor - open DB connection
function __construct() {
    $this->db = new mysqli('localhost', 'username', 'password', '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
    $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
    $stmt->execute();
    $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
    while ($stmt->fetch()) {
        echo "$code has $uses_remaining uses remaining!";
    }
    $stmt->close();
}
}

$api = new RedeemAPI();
$api->redeem();
于 2012-07-15T02:36:02.523 回答
0

为了使用打印进行调试,您可以使用echo如下:

改变:

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

到:

function redeem() {
    // Print all codes in database
    echo "1\n";
    $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
    echo "2\n";
    $stmt->execute();
    echo "3\n";
    $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
    echo "4\n";
    while ($stmt->fetch()) {
    echo "5\n";
        echo "$code has $uses_remaining uses remaining!";
    }
    echo "6\n";
    $stmt->close();
    echo "7\n";
}

这样你就可以看到你在哪条线上失败了。
另一种选择是使用tryand catch

function redeem() {
      try{
        // Print all codes in database
        $stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
        $stmt->execute();
        $stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
        while ($stmt->fetch()) {
            echo "$code has $uses_remaining uses remaining!";
        }
        $stmt->close();
       }
      catch(Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
}
于 2012-07-15T02:25:00.047 回答
0

您是否创建了类的实例?

您应该创建类的实例,然后调用redeem()函数以获得所需的输出。

<?
    $someVar = new RedeemAPI();
    $someVar->redeem();
?>
于 2012-07-15T02:33:53.907 回答
0

这里可能会发生许多不同的事情。在你打开之后

    <?php 

添加

ini_set("display_errors", 1); 

查看错误。通过使用在浏览器中查看结果

http://localhost/promos 

如果代码在您正在执行本教程的机器上运行。如果它在另一台机器上,请将“localhost”替换为远程机器的 IP 地址。从我在你的代码中看到的你没有使用

    $api = new RedeemAPI();
    $api->redeem();

可能就像被排除在外一样简单。如果你添加了这个并且你仍然看到一个空白页面,这可能是由于一个错误或者你在设置 mysql 服务器时错过了一些东西。这是我在完成同一教程时遇到的问题。我将删除数据库表并重新添加它们,确保您按照 mysql 部分的说明添加到“t”。

于 2013-08-18T19:49:24.503 回答