首页 > 解决方案 > 致命错误:未捕获的错误:在 null 上调用成员函数 count()

问题描述

我收到了这个错误,请帮我解决这个问题。我是新手,仍在学习代码,但我仍然无法弄清楚。

错误 :

致命错误:未捕获的错误:在 public_html/subs/short/classes/shortener.php:24 中调用成员函数 count() 堆栈跟踪:#0
/public_html/subs/short/index.php(7): Shortener ->makeURL('https://drive.g...') #1 {main} 在第 24 行的 /public_html/subs/short/classes/shortener.php 中抛出

index.php

<?php
session_start();
require_once "classes/shortener.php";
if(isset($_POST['url'])){
    $url = $_POST['url'];
    $shorten = new Shortener();
    if($code = $shorten->makeURL($url)){
        $fullPath = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']  . $code;
        $fullPath = str_replace("/index.php", "", $fullPath);
        $_SESSION['feedback'] = "Shorten Done! Your new URL is : <a href='{$code}'>" . $fullPath . "</a>";
    } else{
        $_SESSION['feedback'] = "Enter A valid URL!";
    }
    
}
?>

shortener.php

<?php
require_once "db.php";

class Shortener{
    protected $db;
    public function __construct(){
        $this->db = DB::getInstance();
    }
    public function makeCode(){
        $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $randChars = "";
        for($i = 0;$i<5;$i++){
            $rand = random_int(0, strlen($chars)-1);
            $randChars .= $chars[$rand];
        }
        return $randChars;
    }

    public function makeURL($url){
        if(!filter_var($url,FILTER_VALIDATE_URL)){
            return "";
        }
        $exits = $this->db->select("links",array("url","=",$url));
        if($exits->count()){
            return $exits->first()->code;
        } else {
            $randChars = $this->makeCode();
            $this->db->insert("links",array("url" => $url,"code" => $randChars,"date" =>date('Y:m:d H:i:s')));
            return $randChars;
        }   

    }
    public function getURL($code){
        $code = htmlentities($code);
        $code =  $this->db->select("links",array("code","=",$code));
        if($code->count()){
            return $code->first()->url;
        } else {
            return "";
        }
    }
}

提前致谢!

标签: php

解决方案


推荐阅读