我想將我的代碼從mysql轉換爲PDO。雖然我能夠print_r
fetch(PDO::FETCH_ASSOC)
返回的關聯數組,但我仍然無法將任何值(或整個數組)存儲在變量中。儘管所有的錯誤和警告我都無法弄清楚問題所在。另外,我連接的數據庫也在代碼示例中提供。我已經好幾個小時了,但無濟於事。 :( 完整的代碼:如何將單個值或整個數組從存取(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>
PS:我不能讓while循環要麼運行
是否返回你的聯想數組與那些:** {} **? – Matheno
你不能像這樣訪問'id':'$ arr-> id'。 '$ arr'是一個數組,'id'是數組的關鍵。所以你需要這樣:'$ arr [「id」]'。 –
是的,它返回與這些大括號的關聯數組:{} – salmanxk