php 致命错误:调用成员函数 ...on string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30255776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Call to a member function ...on string
提问by Kenziiee Flavius
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.
我不知道为什么这样做,这是什么错误我以前从未遇到过(调用字符串上的成员函数)我使用了与登录时相同的查询/布局等不知道为什么这样做任何答案赞赏。
回答by Gabriel Hautclocq
You're doing :
你正在做的 :
$idea = mysql_real_escape_string($_POST['idea']);
So $idea is a string now. Then you do :
所以 $idea 现在是一个字符串。然后你做:
$check = $idea->checkIdea($title);
There is no checkIdea method on strings.
字符串没有 checkIdea 方法。