我收到此错误
致命错误:未捕获错误:在C:\ xampp \ htdocs \ url \ classes \ shortener.php中调用null上的成员函数escape_string():19堆栈跟踪:#0 C:\ xampp \ htdocs \ url \ shorten.php(10):第19行的C:\ xampp \ htdocs \ url \ classes \ shortener.php中的shortener-> makeCode(' stackove ...')#1 {main}
我正在设计自己的网址缩短程序,但在我测试时它给了我这个错误。 我正在使用的代码
这是获取输入网址的缩短文件 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 {
}
}
这是缩短网址的文件 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){
}
}
答案 0 :(得分:1)
构造需要从两个&#39; _&#39;开始。所以它一定是&#39; __&#39; 。
因此,您需要将public function _construct(){
更改为此public function __construct(){