dongpai2754 2016-05-11 19:12
浏览 38
已采纳

“调用成员函数prepare()”背后的逻辑[重复]

This question already has an answer here:

I'm writing a function to check for records in my database before executing anything, and i'm getting the error Call to a member function prepare() which i don't quite understand. I've been struggeling for quite some time now, and i'd really appriciate some help

The problem should be with the prepare() in line 19

<?php
$dsn = "xxx"; // Database Source Name
$username="xxx"; // User with acress to database. Root is MySQL admin.
$password="xxx"; //The user password.

try {
  $conn = new PDO($dsn, $username, $password);
  $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo "Connection failed: ".$e->getMessage();
}

//------------------------ Does the category already exist? -------------------------
function checkuser($fbid,$fbfname,$fblname,$femail) {

$sql="SELECT COUNT(*) AS subjectcount FROM Users WHERE Fuid=:Fuid";

try {
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(":Fuid",$_SESSION['FBID'], PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $subjectcount=$row["subjectcount"];
    } catch (PDOException $e) {
        echo "Server Error - try again!".$e->getMessage();
    }

//------------------------ If it dosn't, insert it -------------------------
if ($subjectcount==0) {

And i'm having a hard time debugging since i dont quite understand the cause of this error.

I updated my code to

<?php
$dsn = "xxx"; // Database Source Name
$username="xxx"; // User with acress to database. Root is MySQL admin.
$password="xxx"; //The user password.

try {
  $conn = new PDO($dsn, $username, $password);
  $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo "Connection failed: ".$e->getMessage();
}

//------------------------ Does the category already exist? -------------------------
function checkuser($fbid,$fbfname,$fblname,$femail) {
global $conn;
$sql="SELECT COUNT(*) AS subjectcount FROM Users WHERE Fuid=:Fuid";

try {
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(":Fuid",$_SESSION['FBID'], PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $subjectcount=$row["subjectcount"];
    } catch (PDOException $e) {
        echo "Server Error - try again!".$e->getMessage();
        exit;
    }

//------------------------ If it dosn't, insert it -------------------------
if ($subjectcount==0) {
</div>
  • 写回答

1条回答 默认 最新

  • douke3007 2016-05-11 19:15
    关注

    I guess the full error message is:

    Fatal error: Call to a member function prepare() on a non-object
    

    Right?

    You are calling the member function (= function which is a member of a class instance) prepare of the variable $conn which is not declared in the scope of your checkuser function, it's declared outside (in global scope)!

    To "import" it into your function so you can access it, put this line at the top of your function:

    global $conn;
    

    Also, it looks like you don't have full error reporting enabled, otherwise you would have seen this error before the fatal one, giving you another clue:

    Notice: Undefined variable: conn
    

    (By the way, you should exit; after outputing the DB error message in your catch block - otherwise you will print the error but continue to the rest, with a nonexisting DB connection!)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条