所以我正在开发一个 URL 缩短器,在我的数据库中我有一个 id
, url
, code
,和created
。所以基本上我的代码会检查并检查 url
是否输入存在然后返回 code
,但由于某种原因 code
没有被退回。这是我得到的。
我的Shorten.php
类
<?php
class Shortener{
protected $db;
public function __construct(){
$this->db = new mysqli('localhost','root','wayne123','s');
}
protected function generateCode($num){
# code...
}
public function makeCode($url)
{
$url = trim($url);
if(!filter_var($url, FILTER_VALIDATE_URL)){
return '';
}
$url = $this->db->escape_string($url);
//Check if exists
$exists = $this->db->query("SELECT code FROM links WHERE url = '{$url}'");
if($exists->num_rows){
return $exists->fetch_objects()->code;
}
else{
}
}
public function getUrl($code){
# code...
}
}
还有我的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{
//Problem
}
}
最佳答案
您忘记了 $s = new Shortener();
处的 ()
关于php - 数据库中的代码未返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23367033/