Wednesday 3 February 2016

php - Fatal error: Call to a member function ...on string




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



    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





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


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...