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

时间:2018-09-06 00:46:45

标签: php html mysql phpmyadmin

请稍等,请勿将其发布为重复副本,因为我已经完成了研究并尝试了所有方法,但无法使其正常工作,我不断收到此错误“致命错误:在C:\ wamp64字符串上调用成员函数prepare() \ www \ Etego \ dbcontroller.php在第63行上”,我想让人们在我的题词表格上不要重复使用同一封电子邮件两次,谢谢!这是代码:

dbcontroller.php

AND [Person] IN (select * from string_split(@P1,','))

index2.php

<?php
class DBController {
    public $host = "localhost";
    public $user = "root";
    public $password = "";
    public $database = "members";
    public $conn;

    function __construct() {
        $this->conn = $this->connectDB();
    }

    function connectDB() {
        $conn = mysqli_connect($this->host,$this->user,$this->password,$this->database);
        return $conn;
    }

    function runQuery($query) {
        $result = mysqli_query($this->conn,$query);
        while($row=mysqli_fetch_assoc($result)) {
            $resultset[] = $row;
        }        
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysqli_query($this->conn,$query);
        $rowcount = mysqli_num_rows($result);
        return $rowcount;    
    }

    function updateQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query1: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function insertQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query2: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }

    function deleteQuery($query) {
        $result = mysqli_query($this->conn,$query);
        if (!$result) {
            die('Invalid query3: ' . mysqli_error($this->conn));
        } else {
            return $result;
        }
    }
}


/* Email already exists */
/*line 63*/       
$db = new DBController; 
$db->database->prepare("SELECT * FROM members WHERE email = ?");
$reqemail->execute(array($email));
$emailexist = $reqemail->rowCount();
if($emailexist == 0) {
} else {
    $error_message = "Email already exists";
}
//end of email existance
?> 

2 个答案:

答案 0 :(得分:3)

这里有很多问题:

在尝试准备语句的地方,您正在使用$db->database->prepare(),如果您在课堂上查看属性database,则它是一个包含字符串members的字符串,即{{1} } 其中解释了所报告的错误

您似乎还把public $database = "members"; API和mysqli_ API弄混了,并使用了某些PDO API函数,它们是完全不同的野兽,它们将永远无法工作

所以也要更改

PDO

收件人

/* Email already exists */
/*line 63*/       
$db = new DBController; 
$db->database->prepare("SELECT * FROM members WHERE email = ?");
$reqemail->execute(array($email));
$emailexist = $reqemail->rowCount();
if($emailexist == 0) {
} else {
    $error_message = "Email already exists";
}

,您将使用连接对象准备查询以及所有/* Email already exists */ /*line 63*/ $db = new DBController; $stmt = $db->conn->prepare("SELECT * FROM members WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows > 0) { $error_message = "Email already exists"; } API函数,方法和属性。

更新:仍在创建dup帐户

您认为您的重复帐户检查在错误的位置,应移至index2.php中。

或者在此行之后,针对mysqli_添加测试,因为您忘记测试Dup电子邮件检查是否产生了错误。

$error_message

我的强烈建议是在index2中进行Dup Email检查,并将其从if(!isset($error_message)) { require_once("dbcontroller.php"); if ( !isset($error_message) ) { 中删除,因为它并不真正属于dbconnect.php,因为每次您要连接到数据库中的任何脚本!

答案 1 :(得分:1)

问题是您的$database变量是不具有prepare()函数的字符串。相反,您可能想要使用保持有效数据库连接的$conn变量。

为此,请更改

$db->database->prepare("SELECT * FROM members WHERE email = ?");

$stmt = $db->conn->prepare("SELECT * FROM members WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();

这是PHP official documentation