如何修复在字符串上调用成员函数prepare()

时间:2016-08-22 16:02:28

标签: php pdo

我一直在尝试修复此错误:PHP Fatal error: Call to a member function prepare() on string in /home/...../lib/library.php on line 91  我还查看了这个链接:Reference - What does this error mean in PHP? 我似乎并不了解如何将其与我手头的问题联系起来。有人可以帮忙吗?这是受影响的脚本行:

public function Login($username, $password)
    {
        try {
            $db = DB();
            $query = $db->prepare("SELECT user_id FROM users WHERE (username=:username OR email=:username) AND password=:password");
            $query->bindParam("username", $username, PDO::PARAM_STR);
            $enc_password = hash('sha256', $password);
            $query->bindParam("password", $enc_password, PDO::PARAM_STR);
            $query->execute();
            if ($query->rowCount() > 0) {
                $result = $query->fetch(PDO::FETCH_OBJ);
                return $result->user_id;
            } else {
                return false;
            }
        } catch (PDOException $e) {
            exit($e->getMessage());
        }
    }

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

$db = DB();

如果您var_dump() $db DB变量,则表明它等于字符串,其值为DB()$db = new DB(); 。< / p>

应该是:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <complianceLevel>1.8</complianceLevel>
                        <weaveDependencies>
                            <weaveDependency>
                                <artifactId>common-logger</artifactId>
                                <groupId>com.common</groupId>
                            </weaveDependency>
                        </weaveDependencies>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.8.9</version>
                </dependency>
            </dependencies>
        </plugin>