一起使用fetch_column和fetch(PDO :: FETCH_ASSOC)吗? [英] Use fetch_column and fetch(PDO::FETCH_ASSOC) together?

查看:191
本文介绍了一起使用fetch_column和fetch(PDO :: FETCH_ASSOC)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会承认php对我来说是一种新语言.

I will admit php is a new language to me.

现在,我可以分别处理每个问题.在我的准备查询中,SELECT * FROM ...将允许我的PDO提取assoc while循环正常工作,但随后获取列不起作用.然后,我使用SELECT COUNT(*)来获取列,但无法获取assoc.

Now I can get each of these working individually. In my prepare query SELECT * FROM... will allow my PDO fetch assoc while loop to work, but then fetch column doesn't work. And then I use SELECT COUNT(*) my fetch column works but then my fetch assoc doesn't.

那么这周围是否存在,所以两者都可以工作?因为我需要fetch列以整数值(1或0)返回有多少行,以确定用户是否输入了登录信息.但是然后我需要在那儿获取列以返回在数据库表的用户名部分中输入的字符串值.这样我就可以根据用户输入的信息检查此信息,以验证用户名和密码.

So Is there away around this so both will work? As I need the fetch column to return how many rows there are as an integer value (1 or 0) to determine if the user has entered log in information. But then I need fetch column there to return back the string value of what is entered in my username section of the table in my database. So that I can use this information to check it against the input from the user to validate the user and password.

谢谢,这是我的代码.如果您需要更清楚地解释,我会去的.

Thanks, here's my code. If you need it explained more clearly I'll have a go.

<?php
$config['db'] = array(
    'host'      => 'localhost',
    'username'  => 'root',
    'password'  => '',
    'dbname'    => 'inb271assignment'
);

$pdo = new PDO('mysql:host=' . $config['db']['host'] . '; dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try
{
    $pdo->beginTransaction();

    $username = $_POST['Username'];
    $password = $_POST['Password'];

                        //Nicholas will be $dbUsername when fetch is working correctly.
    $databaseusername = Nicholas;

    if ($username&&$password)
    {
        $result = $pdo->prepare("SELECT COUNT(*) FROM members WHERE Username=?");
        $result->execute(array($databaseusername));

        $row = $result->fetchColumn();

        if ($row!=0) {

            while ($rows = $result->fetch(PDO::FETCH_ASSOC)) {

                $dbUsername = $rows['Username']; 


            }
            echo $dbUsername;

        }
        else
            die("That user doesn't exist");

    }

   $pdo->commit();
}
catch(PDOException $pe)
{
    echo($pe->getMessage());
}
?>

所以目前我那里有SELECT COUNT(*).因此,如果我在页面上的表单中输入用户名和密码,然后用户名称和密码将以!= 0的形式返回,则while循环将起作用.如果我有SELECT *,则while循环通常可以正常工作.但是因为我不需要,因为我不需要计数.因此,我无法从数据库中检索所需的信息.

So currently I have SELECT COUNT(*) in there. So if I enter a username and password in my form on the page before it will return back as !=0 allowing the while loop to work. And the while loop normally works if I have SELECT *. But because I don't because I need the count it doesn't. So I can't retrieve the info I need from the database.

推荐答案

正常使用SELECT * FROM ...和PDO提取assoc,并使用$result->rowCount();返回所有受影响的行,与SELECT COUNT(*)等效.

Use SELECT * FROM ... and PDO fetch assoc as normally and use $result->rowCount(); for returning all affected rows, which is equivalent with SELECT COUNT(*)

这篇关于一起使用fetch_column和fetch(PDO :: FETCH_ASSOC)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆