致命错误:在字符串上调用成员函数....

时间:2015-05-15 09:17:49

标签: php mysql

连接在这里

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;
}
?>

我怀疑它的连接,但只是因为......它一直在为所有其他查询工作,但谁知道。

其次,包含就像这样

    <?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());
    ?>

这里的倒数第二是我在类

中的查询
<?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;
    }
?>

现在最后这是我在主页上调用的功能!

<?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!";
              }
            }
          ?>

我不知道它为什么这样做,这个错误我之前从未遇到过(调用字符串上的成员函数)我使用了与登录时相同的查询/布局等等,不知道为什么这样做任何答案都赞赏。

1 个答案:

答案 0 :(得分:9)

您正在做:

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

所以$ idea现在是一个字符串。 然后你做:

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

字符串上没有checkIdea方法。