我收到此錯誤致命錯誤:未捕獲的錯誤:調用()成員函數escape_string上的空
Fatal error: Uncaught Error: Call to a member function escape_string() on null in C:\xampp\htdocs\url\classes\shortener.php:19 Stack trace: #0 C:\xampp\htdocs\url\shorten.php(10): Shortener->makeCode('stackove...') #1 {main} thrown in C:\xampp\htdocs\url\classes\shortener.php on line 19
我設計我自己的網址縮短服務,但是當我測試它,它給了我這個錯誤。 我使用這個
此代碼是縮短文件,以獲得輸入網址 shorten.php
<?php
session_start();
require_once 'classes/shortener.php';
$s = new Shortener;
if(isset($_POST['url'])) {
$url = $_POST['url'];
if($code = $s->makeCode($url)) {
echo $code;
} else {
}
}
這是一個縮短的URL shortener.php文件
<?php
class Shortener{
protected $db;
public function _construct(){
$this->db = new mysqli('localhost','root','','shortenedurl');
}
protected function generateCode($num){
return base_convert($num, 10, 36);
}
public function makeCode($url){
$url = trim($url);
if(!filter_var($url, FILTER_VALIDATE_URL)) {
return '';
}
$url = $this->db->escape_string($url);
$exists = $this->db->query("SELECT code FROM short_urls WHERE urlinput = '{$url}'");
if($exists->num_rows) {
return $exists->fetch_object()->code;
} else {
$insert = $this->db->query("INSERT INTO short_urls(urlinput, created) VALUES ('{$url}', NOW())");
$code = $this->generateCode($this->db->insert_id);
$this->db->query("UPDATE short_urls SET code ='{$code}' WHERE url ='{$url}'");
return $code;
}
}
public function getUrl($code){
}
}
是!那麼你的問題是什麼? – RiggsFolly
爲什麼我得到這個錯誤.. –
致命錯誤:未捕獲錯誤:調用C:\ xampp \ htdocs \ url \ classes \ shortener.php中的null成員函數escape_string():19堆棧跟蹤:#0 C: \ xampp \ htdocs \ url \ shorten.php(10):Shortener-> makeCode('http:// stackove ...')#1 {main}拋出C:\ xampp \ htdocs \ url \ classes \ shortener。在線19上的PHP –