Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP教程代码不工作_Php_Database - Fatal编程技术网

PHP教程代码不工作

PHP教程代码不工作,php,database,Php,Database,遵循在线教程并设法获得以下代码: class RedeemAPI { private $db; // Constructor - open DB connection function __construct() { $this->db = new mysqli('localhost', 'username', 'password', 'promos'); $this->db->autocommit(FALSE); } // Destructor - clo

遵循在线教程并设法获得以下代码:

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%正确

有什么想法吗?
谢谢

为了使用打印进行调试,您可以使用如下
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();
}
致:

这样,您可以看到您在哪一行失败。
另一个选项是使用
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";
      }
}

您是否创建了类的实例

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

<?
    $someVar = new RedeemAPI();
    $someVar->redeem();
?>

您的代码应该包括:
$api=new-api()和<代码>$api->兑换(),只是因为教程没有包括进一步向下的内容并不意味着您不使用它

因此,请尝试:

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();

这里可能会发生很多不同的事情。就在你开幕之后

    <?php 
查看错误。使用在浏览器中查看结果

http://localhost/promos 
如果代码是在您正在执行本教程的机器上运行的。如果它位于另一台计算机上,请将“localhost”替换为远程计算机的ip地址。从我看到的代码中,您没有使用

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

它可能是如此简单的事情被排除在外。如果您添加了此选项,但仍然看到一个空白页,则可能是由于设置mysql服务器时出现错误或遗漏了某些内容。这是我在完成本教程时遇到的问题。我将删除数据库表并重新添加它们,确保您按照mysql部分的说明添加到“t”。

打开错误(运行:
error\u reporting(-1);
)或添加打印以查看哪一行出现故障,并且您的代码包括
$api=new-api()
和您的调用
$api->redemble()?@alfasin PHP新手(因此我为什么要学习教程)如何让它打印出错误?@Lawrence Cherone我在代码中看不到这一点?看起来像是这样的图坦卡蒙:调用该代码就是在第一个代码块中运行类的代码;轻拍一下——我在浏览器中运行它——就像它在教程中说的那样,它应该返回:“测试还有10000次剩余使用!”;您正在浏览器中运行此php?然后您应该在php脚本的末尾添加Lawrence上面提到的两行代码:
$api=new-api()$api->赎回()
@MarkP页面中的所有代码都定义了类和方法。当你添加这两行代码时,你会创建一个类的实例并调用你想要运行的方法!我一直在学习教程,但我自己知道的还不够多。但是,伙计们,非常感谢你们在这个问题上帮助我,并提供了额外的帮助。非常感谢:)
http://localhost/promos 
    $api = new RedeemAPI();
    $api->redeem();