Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用PDO在单个查询中获取行数和该行的数据_Php_Mysql_Sql_Pdo - Fatal编程技术网

在PHP中使用PDO在单个查询中获取行数和该行的数据

在PHP中使用PDO在单个查询中获取行数和该行的数据,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我有一个登录系统,最初我检查用户名/密码组合是否正确,然后获取列以设置会话变量 这是我的密码 $sql='select uid,name from users where username=? and password=?'; $query=$con->prepare($sql); $result=$query->execute(array($username,$password)); $query->setFetchMode(PDO::FETCH_ASSOC); //che

我有一个登录系统,最初我检查用户名/密码组合是否正确,然后获取列以设置会话变量

这是我的密码

$sql='select uid,name from users where username=? and password=?';
$query=$con->prepare($sql);
$result=$query->execute(array($username,$password));
$query->setFetchMode(PDO::FETCH_ASSOC);

//check if a single row is returned 

if(($query->fetchColumn())===1)
    {
        //set session variables
        header('Location: lading_page.php');
    }
else echo "Invalid username/password";
1)
fetchColumn()
给出了错误的结果,结果是3(尽管我有一行匹配该参数,每行15个coulmns),但是如果
$sql

$sql='select count(*)from username=?密码=?'

给出正确答案为1为什么

2)我看过php手册,发现
rowCount()
仅适用于DML查询,它建议,但每次登录需要2次查询

有没有一种方法可以使用单一查询

编辑

现在我正在使用类似的东西,它似乎工作得很好,但没有什么问题。请参阅评论

$sql='select uid,name from users where username=? and password=?';
$query=$con->prepare($sql);
$result=$query->execute(array($username,$password));

$rows= $query->fetchAll(PDO::FETCH_ASSOC);
if(count($rows)===1)
    {
          //I reached here
        echo $_SESSION['id']=$rows['uid']; //undefined index uid
        echo $_SESSION['name']=$rows['name'];//undefined index name
        //header('Location: landing_page.php');
    }
else $error="Invalid username/password";

希望这件事你说对了

用fetchall试试吧

$rows = $query->fetchAll(PDO::FETCH_ASSOC);
就你而言

$sql='select uid,name from users where username=? and password=?';
$query=$con->prepare($sql);
$result=$query->execute(array($username,$password));


// check if a single row is returned
// i guess to avoid the warnings you can just set the array befor like
// $row = array();
// or put @ befor the VARS in the TRUE statement

$rows = $query->fetchAll(PDO::FETCH_ASSOC));

/** 
if you have more than one result you can look at them like this

foreach ($rows as $row)
{
    echo $row['uid'];
}
**/

if((count($rows)===1)
    {
        echo $_SESSION['id']=@$rows['uid']; 
        echo $_SESSION['name']=@$rows['name'];
        // header('Location: lading_page.php');
    }
else echo "Invalid username/password";

fetchColumn()-从结果集的下一行返回单个列。它不是像“select count(*)…”中那样的结果行数,而是结果行中(在您的示例中)第一列(uid)的值

现在,我将如何访问返回行的列值。是否需要执行另一个查询?假设列为
uid
name
。请更新该列。:/code>echo$query->fetchAll(PDO::FETCH\u ASSOC)这返回的是一个数组而不是一个数字。我也应该使用count()吗?请参阅编辑。即将完成的代码:)顺便说一句,这些不是错误,这些是警告。因为这场战争从来没有发生过。只需在变量前添加一个@,然后根据您的编辑查看我的编辑:Is
session_start()在所有页面中使用,会话名称/变量是否已分配?@Fred ii-:是的。但是当执行
var_dump()时,我得到了未定义的索引$\u会话
$\u会话['id']
$\u会话['name']
上查看显示的内容。如果没有显示任何内容,则说明它们的分配不正确,或者不在正确的位置。@Fred ii-:正如我所说,我在执行$rows['uid']
时得到了未定义的索引uid。
var\u dump()
将其显示为null。问题在于访问
$rows['uid']中的列值
。请帮助不要在
if(count($rows)==1)中有3个
==
您可以尝试
if(count($rows)==1)
if(count($rows)>0)