0

So I'm working on a URL shortener and in my database I have an id,url,code, and created. So basically my code goes through and checks if the url entered exists then returns the code, but for some reason the code isn't being returned. Here's what I got.

My Shorten.php class

  <?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...
}

}

And my 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
}

 }
3
  • In the false case you're not returning anything.. are you? Commented Apr 29, 2014 at 14:07
  • Well I have http://www.google.com in the DB, so it should echo google123. And not returning at the moment @briosheje Commented Apr 29, 2014 at 14:13
  • you have no $code var in the second php page Commented Apr 29, 2014 at 14:35

1 Answer 1

2

you forgot the () at the $s = new Shortener();

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm. That didn't seem to fix the issue.
add () to the new object.
Still not solving the issue.
you have done both things? changed the name and added ()? try adding another = to that line if($code = $s->makeCode($url)){
so tell me more, whats not working? what error do you get? does it goes to the else, or just not showing $code? try print_r($code)