duanguan1573 2015-11-17 12:54
浏览 77
已采纳

PHP错误 - 在非对象上调用成员函数execute()

I wrote thte following piece of code:

<?php

$username = $_POST['user'];
$password = $_POST['pass'];

$db = new PDO ('mysql:host=localhost;dbname=ozdatabase;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, 'ERRMODE_EXCEPTION');

$stmt = $db->prepare("SELECT id, users FROM ozusers WHERE username=? AND password=?");
$stmt->execute(array($username, $password));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$id = $rows['id'];
$user = $rows['users'];

if ($id) {
    print "Logged";
}

else {
    print "not good";
}

?>

This is the HTML Form:

<form id='login' action='login.php' method='post' accept-charset='UTF-8'>
<fieldset >
<LEGEND>COMMUNICATION</LEGEND>
<input type='hidden' name='submitted' id='submitted' value='1' />

<label for='username' >UserName*:</label>
<input type='text' name='user' id='username' maxlength="50" />

<label for='password' >Password*:</label>
<input type='password' name='pass' id='password' maxlength="50">

<input type='submit' name='Submit' value='Submit' />

</fieldset>
</form>

I get an error when trying to login in the page and it's written: "Fatal error: Call to a member function execute() on a non-object in.. On line 15"

Why is this happening? I followed best practice guide and it showed to use the "execute()" function exactly like that..

Thanks

  • 写回答

3条回答 默认 最新

  • dongqiu3709 2015-11-17 13:24
    关注

    ERRMODE_EXCEPTION is constant wrap off quotes from it

    $db->setAttribute(PDO::ATTR_ERRMODE, ERRMODE_EXCEPTION);//Your code fails at this line
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助