dounangqie4819 2014-09-27 00:59
浏览 24
已采纳

使用SELECT获取正确的行,在PDO中获得零行

I have a serious problem

I'm mounting a REST API with PHP. So, I have a controller called "Checkestablishment" (this will check the username and password from a database).

I have the next Query

SELECT table_users.ID, table_users.FirstName, table_users.LastName, table_users.ZIP,     table_users.State, table_users.Country, table_users.Email, table_users.Birthday
FROM table_users, table_user_credentials
WHERE table_user_credentials.User = table_users.ID
AND table_user_credentials.Username = 'someuser'
AND table_user_credentials.Pass = 'somepass'

And it works fine, MySQL gets me the desired row.

But when i'm trying to execute it in PDO, I'm getting null.

This is my code

    public function add()
    {
        global $counect;
        $i = 0;
        $Query_CheckCredential = "SELECT table_users.ID, table_users.FirstName, table_users.LastName, table_users.ZIP,     table_users.State, table_users.Country, table_users.Email, table_users.Birthday
FROM table_users, table_user_credentials
WHERE table_user_credentials.User = table_users.ID
AND table_user_credentials.Username = ':Est_User'
AND table_user_credentials.Pass = ':Est_Pass'";
        $PS_CheckCredential = $counect->prepare($Query_CheckCredential);
        $Sucess = $PS_CheckCredential -> execute(array(
                                    ':Est_User'=>$this->str_User,
                                    ':Est_Pass'=>$this->str_Pass
                                    ));
        if($Sucess)
        {
            $PS_CheckCredential -> bindColumn(1, $User_ID);
            $PS_CheckCredential -> bindColumn(2, $User_FirstName);
            $PS_CheckCredential -> bindColumn(3, $User_LastName);
            $PS_CheckCredential -> bindColumn(4, $User_ZIP);
            $PS_CheckCredential -> bindColumn(5, $User_State);
            $PS_CheckCredential -> bindColumn(6, $User_Country);
            $PS_CheckCredential -> bindColumn(7, $User_Email);
            $PS_CheckCredential -> bindColumn(8, $User_Birthday);
            //$PS_CheckCredential -> setFetchMode(PDO::FETCH_ASSOC);

            $Array_Est_Keys = array('ID', 'FirstName', 'LastName', 'ZIP', 'State', 'Country', 'Email', 'Birthday');

            $Array_Est_Keys = array_fill_keys($Array_Est_Keys, '');

            do
            {
                $Array_User['ID'] = $row['ID'];
                $Array_User['FirstName'] = $User_FirstName;
                $Array_User['LastName'] = $User_LastName;
                $Array_User['ZIP']  = $User_ZIP;
                $Array_User['State'] = $User_State;
                $Array_User['Country'] = $User_Country;
                $Array_User['Email'] = $User_Email;
                $Array_User['Birthday'] = $User_Birthday;
            }
            while($row = $PS_CheckCredential->fetch(PDO::FETCH_ASSOC));
            if(empty($Array_User))
            {
                return false;
            }
            else
            {
                return $Array_User;
            }
        }
        else
        {
            echo "No luck";
            //return false;
        }

I tried to fetch the query by different ways, but no one solved my problem.

P.D. Sucess marks me as true, because the execution of the query was successfully, but i'm getting null (i made a print_r() for the array that i'm creating and i'm getting all the array with values in null).

Thanks for your time. Cheers!

  • 写回答

2条回答 默认 最新

  • duankang5882 2014-09-27 01:14
    关注

    Remove the quotes around ':Est_User'
    in AND table_user_credentials.Username = ':Est_User'

    and around ':Est_Pass' in AND table_user_credentials.Pass = ':Est_Pass'

    Those binds in the SELECT query should not contain quotes.

    Having checked for errors would have signaled it.

    Such as:

    $counect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    right after the DB connection is opened.

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

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败