Home » Php » php – Fatal error: Call to a member function …on string

php – Fatal error: Call to a member function …on string

Posted by: admin July 12, 2020 Leave a comment


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


  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


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!

            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']);
                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.

How to&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.