10

Connection is here

class connection{

private $hostname = "localhost";
private $username = "root";
private $password = "";
private $database = "idea";
private $conn;

public function __construct(){
    $this->conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("Error Connection To MySQL");
}

public function getConn(){
    return $this->conn;
}
?>

I doubt that its the connection but just incase... its been working for all other queries but who knows.

Secondly the includes are all here like so

    <?php 
session_start();

  if ($_SESSION['loggedin'] != 1) {
    header('location: index.php');
  }

    include 'connection.php';
    include 'users.php';
    include 'ideas.php';
    $conn = new connection();
    $user = new users($conn->getConn());
    $idea = new ideas($conn->getConn());
    ?>

Second to last here is my query inside a class

<?php 

class ideas{

    private $conn;

    public function __construct($db){
        $this->conn = $db;
    }

    public function checkIdea($title){
        $result = $this->conn->query("SELECT * FROM ideas WHERE title = '$title'");
        return $result;
    }
?>

And now lastly here is the function that i call on the homepage!

<?php 
            if (isset($_POST['addidea'])) {
              $title = mysql_real_escape_string($_POST['title']);
              $idea = mysql_real_escape_string($_POST['idea']);

              $check = $idea->checkIdea($title); // <-- sais this is the error here...

              if ($check->num_rows == 0) {
                echo $idea->getUserId($_SESSION['username']);
              }else{
                echo "Sorry that iDea title is already taken, please use another!";
              }
            }
          ?>

I have no idea why its doing this, what is this error i have never come accross it before (call to member function on string) ive used the same query/ layout as i did for the login etc no idea why its doing this any answers appreciated.

Foad Tahmasebi
  • 1,333
  • 4
  • 16
  • 33
Kenziiee Flavius
  • 1,918
  • 4
  • 25
  • 57

1 Answers1

12

You're doing :

$idea = mysql_real_escape_string($_POST['idea']);

So $idea is a string now. Then you do :

$check = $idea->checkIdea($title);

There is no checkIdea method on strings.

Gabriel Hautclocq
  • 3,230
  • 2
  • 26
  • 31
  • 2
    I stumbled onto this Q&A from a Google search related to another question. What's also going on with the OP's code is that they connnected using the `mysqli_` api but used `mysql_` functions that don't intermix. The "technically correct" usage should have been `$idea = mysqli_real_escape_string($connection, $_POST['idea']);` and the same for the others. – Funk Forty Niner Mar 04 '17 at 22:37