doupingzhi9674 2016-04-20 15:12
浏览 114
已采纳

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

I'm working on a website; apache asks me to upgrade to the new method of PDO.

I have my function which will collect all the information of user X, indicated by the variable $_SESSION['username'].

My problem is: I get the error title. However I want to collect String and INT values.

My Function: functions.php

function getDetails($username) {
    $db = require("dbConn.php");
    $sql = $db->prepare("SELECT * FROM `es42_members` WHERE HandleName='$username'");
    $sql->execute();
    $row = $sql->fetchAll();

    return $row;
}

My index file: index.php

$details = getDetails($_SESSION['username']);

while ($row = $details->fetch_assoc())  {
    $id = $row['UserID'];
    $email = $row['Email']; // This return String
    $isAdmin = $row['isAdmin']; //This return 0 - 1. But in the table is registered by String (Varchar).
}

echo "Your email is: $email";
if($isAdmin == "1"){ echo "You are a admin!" }

UPDATE My new code is as follows:

Function: @Marcus

function getDetails($username) {
    require("dbConn.php");
    $sql = $db->prepare("SELECT * FROM `es42_members` WHERE HandleName='$username'");
    $sql->execute();
    $row = $sql->fetchAll();

    return $row;
}

dbConn.php [Original]

<?php 

$db = new PDO('mysql:host=localhost;dbname=ProfileTest', "root", "1234");
$db = null;

?>

dbConn.php [Edited and working]

<?php 

$db = new PDO('mysql:host=localhost;dbname=ProfileTest', "root", "1234");

?>
  • 写回答

2条回答 默认 最新

  • dt2002 2016-04-20 15:21
    关注

    This is happening because $db isn't actually holding the PDO connection object. You've assigned the dbConn.php script itself (returns 1 on successful inclusion) to $db, which is why you're receiving the integer error in question.

    Your dbConn.php file should look something like the following (please add dbConn.php to your original post for verification):

    // dbConn.php
    $db = new PDO($dsn, $user, $password); // PDO has been instantiated and $db is holding the object
    

    Then in your functions.php script:

    function getDetails($username) {
        require("dbConn.php"); // require me, don't assign me to a variable
        $sql = $db->prepare("SELECT * FROM `es42_members` WHERE HandleName='$username'");
        $sql->execute();
        $row = $sql->fetchAll();
    
        return $row;
    }
    

    You'd simply require dbConn.php and NOT assign it to any variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。