php - 致命错误:未捕获的错误:在 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 "";
}
}
}
提前致谢!
解决方案
推荐阅读
- facebook - 抓取公开的 Facebook 帖子
- android - 访问 OnKeyListener 内部类中的数组
- spring-boot - 在 Spring Boot 中-您可以在不进行任何解析/干预的情况下进行接收整个消息正文的请求映射吗?
- jsf - 如何手动将 HTTP 请求转发到默认 Faces Servlet
- javascript -
- mysql - MYSQL: can BULK select can still return array of results from inputs of same value?
- ios - UIView - What override will allow updating a UIView frame
- javascript - Axios GraphQl query throws internal server error but fetch() works
- angular - Clicking on specific ion-item button triggers the other buttons in ionic 3
- python - 程序不会更改字符串中的字母