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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?