dqmdlo9674 2018-01-01 16:09 采纳率: 100%
浏览 81
已采纳

为什么我的pdo变量始终未定义? [重复]

I have two php files, one is a file for a PDO Connection and one is a file in which a prepared statement is submitted on this connection:

PDO Connection:

class Connection {

protected $user = "ni873420_2sql1";
protected $pass = "#";
public $pdo_log;

public function __construct() {

        try {

            $pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                               $this->user, $this->pass);

        }
        catch (Exception $e) {

            die($e);

        }
    }
}

And this is the function where I create the prepared statement:

    public static function getInfobyEmailUsername($argemailusername) {

    $get_info_query = "SELECT * FROM user WHERE email = ? OR username = ?";

    $conn = new Connection();
    $getinfostmt = $conn->pdo_log->prepare($get_info_query);
    $getinfostmt->execute(array($argemailusername, $argemailusername));

    $userinfo = $getinfostmt->fetch(PDO::FETCH_ASSOC);

    if (!empty ($userinfo)) {
        return $userinfo;
    } else {
        return false;
    }

    }

But I keep getting the error:

Fatal error: Call to a member function prepare() on null

I cant find a solution anywhere, can anyone help me with this or at least send a link where I can find the solution?

Thank you!

</div>
  • 写回答

1条回答 默认 最新

  • dongqin1167 2018-01-01 16:13
    关注

    In your construct function, use the public variable $pdo_log

      $this->pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                               $this->user, $this->pass);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题