Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Call to a member function ...on string [duplicate]

Tags:

php

mysql

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.

like image 801
Kenziiee Flavius Avatar asked May 15 '15 09:05

Kenziiee Flavius


1 Answers

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.

like image 135
Gabriel Hautclocq Avatar answered Nov 20 '22 08:11

Gabriel Hautclocq