gpt4 book ai didi

php - 如何将 fetch(PDO::FETCH_ASSOC) 中的单个值或整个数组存储在变量中?

转载 作者:行者123 更新时间:2023-12-01 00:39:51 26 4
gpt4 key购买 nike

我正在尝试将我的代码从 mysql 转换为 PDO。虽然我能够 print_r fetch(PDO::FETCH_ASSOC) 返回的关联数组,但我仍然无法存储来自它在一个变量中。尽管有所有错误和警告,但我无法弄清楚问题出在哪里。此外,代码示例中也提供了我要连接的数据库。我已经研究了几个小时,但无济于事。 :(完整代码:

<!DOCTYPE html>
<htmL>
<head>
<title>Login Page</title>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<?php
/*
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `admin` (
`id` int(11) NOT NULL,
`username` varchar(24) NOT NULL,
`password` varchar(24) NOT NULL,
`last_log_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `admin` (`id`, `username`, `password`, `last_log_date`) VALUES
(1, 'salman', '123', '2016-02-13');
ALTER TABLE `admin`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username` (`username`);
ALTER TABLE `admin`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

*/
session_start();
if(isset($_POST["username"]) && isset($_POST["password"])) {
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]);
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = '';
try {
$dbh = new PDO("mysql:host=$db_hostname;dbname=store_db", $db_username, $db_password);
}
catch(PDOException $e)
{
echo "Error!:". $e->getMessage() . "<br/>";
die();
}
$res = $dbh->prepare("SELECT id FROM admin WHERE username='salman' AND password='123' LIMIT 1"); // username='$manager' AND
$res->bindParam(':manager', $manager, PDO::PARAM_INT);
$res->bindParam(':password', $password, PDO::PARAM_STR);
$res->execute();
print_r($res->fetch(PDO::FETCH_ASSOC));//DEBUG PURPOSE ONLY: prints "{Array ( [id] => 1 ) }" meaning data has been successfully retrieved
$arr = $res->fetch(PDO::FETCH_ASSOC);
echo "the id is:[".$arr['id']."]<br>";//DEBUG PURPOSE ONLY: prints "the id is:[]" meaning the value is not stored in the $arr variable
echo $arr->id;//ERROR: Notice: Trying to get property of non-object in C:\xampp\htdocs\paginator\storeadmin\so.php on line 47
$existCount=$res->rowCount();
if((int)$existCount==1){
while($row = $res->fetchObject()){
$id = $row->id;
echo "aaaaaaaaaaaaaa";//DEBUG ONLY: shows if the while loop actually works (it does not)
}
$_SESSION["id"] = $id; //ERROR:Notice: Undefined variable: id in C:\xampp\htdocs\paginator\storeadmin\so.php on line 54
$_SESSION["manager"] = $manager;
$_SESSION["password"] = $password;
echo "id:[".$_SESSION["id"]."]user:[".$_SESSION["manager"]."]pass:[".$_SESSION["password"]."]";
}
else {
echo 'That information is incorrect, try again <a href="index.php"> Here </a>';
exit();
}
}
?>
</head>
<body>
<?php //include_once("../header.php"); ?>
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-titles">Admin Login</h2>
</div>
<form id="form1" name="form1" method="post" action="so.php" class="panel-body">
<div class="form-group">
<label for="username">Username</label>
<input name="username" type="text" class="form-control" id="username" size="40" placeholder="Username"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" type="password" class="form-control" id="password" size="40" placeholder="Password" />
</div>
<input name="button" type="submit" class="btn btn-default" id="button" value="Log In" />
</form>
</div>
</div>
<div class="col-md-2"></div>
</div>
</body>
</htmL>

P.S:我也无法运行 while 循环。

最佳答案

print_r($res->fetch(PDO::FETCH_ASSOC));

获取第一条记录

$arr = $res->fetch(PDO::FETCH_ASSOC);

尝试获取下一个记录....但是没有,您已经使用print_r($res ->fetch(PDO::FETCH_ASSOC)) 语句,所以结果集游标已经移动到第二条记录,没有第二条记录,所以什么也抓不到

关于php - 如何将 fetch(PDO::FETCH_ASSOC) 中的单个值或整个数组存储在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com