dongsheng1698 2012-12-25 00:46
浏览 162

使用fetchAll()从PDO对象中进行选择

For some reason the following code always returns true, no matter what the parameter is and what is actually inside of the SQL row. It also throws an error "Notice: Undefined offset: 0 in C:\wamp\www\Social Networking\INC\login.inc on line 7" but I do not see what is wrong:

<?php
function checkAccount($username, $password){
    include("INC/dbconnect.inc");/*"INC/dbconnect.inc" is <?php $pdo = new PDO("mysql:host=localhost;dbname=socialnetwork","user","123"); ?>*/
    $select = $pdo->prepare("SELECT id,password FROM users WHERE user_username = :username");
    $select->execute(array(':username'=>$username));
    $q_rows = $select->fetchAll();
    if($q_rows[0][0]/*the ID of the user, it should always be greater than 1, if not then the username does not exist*/ > 0 && $q_rows[0][0] != null){
        if($q_rows[0][1]/*the password of the user*/ == $password)
            return true;
        else
            return false;
    }
    else
        return false;
    $pdo=null;
} ?>

Can someone please tell me what is wrong? I have commented inside of the code the problems I am experiencing, and I have tried normal $select->fetch() instead of $select->fetchAll() to no avail. I have read up on PDO before posting this (http://php.net/manual/en/pdostatement.fetchall.php). Here is the rest of my file http://pastebin.com/YCkrRivs, thanks.

  • 写回答

2条回答 默认 最新

  • doujiayao8433 2012-12-25 01:01
    关注

    If the database returns no rows, then there will be no entry $q_rows[0]. Hence, undefined offset 0 as you are trying to retrieve a row in array that doesn't exist. Your 'ID' not > 0 for non-existent user is not a correct summary.

    Sample return looks like this (if you print_r()'d it)

    User present:

    $q_rows = Array (
      [0] => Array ( 
       [id] => 1,
       [password] => 'dno23n3io3'
      )
    )
    

    No user present:

    $q_rows = Array (
    )
    

    You should do:

    if(size($q_rows) > 0) {
       //Handle user present
    } else {
       //Handle no user present
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图