一起使用 fetch_column 和 fetch(PDO::FETCH_ASSOC) ?
我承认 php 对我来说是一门新语言.
I will admit php is a new language to me.
现在我可以让这些中的每一个单独工作.在我的准备查询中 SELECT * FROM... 将允许我的 PDO fetch assoc while 循环工作,但随后 fetch 列不起作用.然后我使用 SELECT COUNT(*) 我的 fetch 列有效,但我的 fetch 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 fetch 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(*)
相关文章