在线教程并设法获取此代码:
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%正确。
任何想法? 谢谢。
答案 0 :(得分: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();
答案 1 :(得分: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";
}
通过这种方式,您可以看到失败的哪一行
另一种选择是使用try
和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";
}
}
答案 2 :(得分:0)
您是否创建了类的实例?
您应该创建一个类的实例,然后调用redeem()
函数以获得所需的输出。
<?
$someVar = new RedeemAPI();
$someVar->redeem();
?>
答案 3 :(得分:0)
这里可能会发生许多不同的事情。在你开幕后
<?php
添加
ini_set("display_errors", 1);
查看错误。使用
在浏览器中查看结果http://localhost/promos
如果正在您正在执行本教程的计算机上运行代码。如果它在另一台机器上,则将“localhost”替换为远程机器的IP地址。从我在你的代码中看到的你没有使用
$api = new RedeemAPI();
$api->redeem();
这可能是一件简单的事情,因为这被排除在外。如果你添加了这个并且你仍然看到一个空白页面,可能是由于在设置mysql服务器时出错或者你错过了什么。完成同样的教程时,这是我的问题。我将删除数据库表并重新添加它们,确保按照't'的mysql部分的说明进行操作。