您好我在使用PDO创建个人资料页面时出现问题 - 我可以使用旧的mysql中的一个工作但我无法弄清楚如何将数据更改为PDO - 我已经在这几个星期,现在只是不知所措。请你好,我对这一切都很新。以下是可行的代码 - 旧方式
<?php
require_once('connection.php');
$id=$_SESSION['SESS_MEMBER_ID'];
$result3 = mysql_query("SELECT * FROM member where mem_id='$id'");
while($row3 = mysql_fetch_array($result3))
{
$fname=$row3['fname'];
$lname=$row3['lname'];
$address=$row3['address'];
$contact=$row3['contact'];
$picture=$row3['picture'];
$gender=$row3['gender'];
}
?>
这就是我试图提出的,但我只是得到一个空白的屏幕
<?php
require_once('connectpdo.php');
$id=$_SESSION['SESS_MEMBER_ID'];
$result3 = $db->prepare("SELECT * FROM member where mem_id='$id'");
$row3 = $stmt->fetch (PDO::FETCH_ASSOC);
{
$fname=$row3['fname'];
$lname=$row3['lname'];
$address=$row3['address'];
$contact=$row3['contact'];
$picture=$row3['picture'];
$gender=$row3['gender'];
}
?>
登录在旧的MySql和PDO中都能正常工作,但我似乎无法在PDO中获得用户配置文件
//Create query
$result = $conn->prepare("SELECT * FROM member WHERE username= :xtxt AND password= :ztzt");
$result->bindParam(':xtxt', $username);
$result->bindParam(':ztzt', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
//Check whether the query was successful or not
if($rows > 0) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['loggedin'] = true;
$_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
$_SESSION['SESS_LAST_NAME'] = $member['password'];
$_SESSION['SESS_USERNAME'] = $member['username'];
session_write_close();
header("location: home.php");
exit();
我得到PHP警告:mysql_fetch_assoc()期望参数1是资源,对象是......从这一行给出的
$member = mysql_fetch_assoc($result);
我得到未定义的变量:stmt in ...&amp; PHP致命错误:从......这一行中的非对象上调用成员函数fetch()
$row3 = $stmt->fetch (PDO::FETCH_ASSOC);
正如我之前提到的,我对此非常陌生,而且我花了很多时间研究做了多个教程,并试图弄清楚这一切。当我收到折旧错误时,我正在学习MySql,现在我切换到PDO,我知道将来我需要从一个登录用户的多个数据库中提取信息。我没有使用函数或类。在此先感谢,我的狗会感谢你的帮助。
答案 0 :(得分:0)
好的,我解决了!我将发布任何其他有此问题的代码@Fred,谢谢!
而且我确实知道代码真的不同,因为我设法在我的测试网站上获取它,在我的登录进程页面中我更改了此代码:
// query
$result = $conn->prepare("SELECT * FROM customers WHERE username=:hjhjhjh AND customers_password= :asas");
$result->bindParam(':hjhjhjh', $username);
$result->bindParam(':asas', $customers_password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($rows > 0) {
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
$_SESSION['customers_id']=$row['customers_id'];
$_SESSION['SESS_CUSTOMERS_KENNEL'];//kwaon ang id sang may tyakto nga username kag password
header("location: http://localhost:8888/ShowMyDog/newtemplate/index.php?action=account");
}
在页面中我想要显示配置文件,我将代码更改为:
<?Php
session_start();
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "Welcome to the member's area, " . $_SESSION['username'] . "!";
} else {
echo "Please log in first to see this page.";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?Php
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
require "config.php"; // Database connection details.
$customers_id=$_SESSION['loggedin']; // Collecting one record with for user
$count=$dbo->prepare("select * from customers where customers_id=:customers_id");
$count->bindParam(":customers_id",$customers_id,PDO::PARAM_INT,1);
if($count->execute()){
echo " Success <br>";
$row = $count->fetch(PDO::FETCH_ASSOC);
print_r($row);
echo "<hr>";
echo "<br>Admin id = $row[customers_id]";
echo "<br>username = $row[username]";
echo "<br>password=$row[customers_password]";
echo "<br>kennel=$row[customers_kennel]";
echo "<br>first name=$row[customers_firstname]";
echo "<br>last name=$row[customers_lastname]";
echo "<br>email address=$row[customers_email_address]";
echo "<br> postcode=$row[customers_postcode]";
echo "<br> street address=$row[customers_street_address]";
echo "<br> gender=$row[customers_gender]";
}
?>
</body>
</html>
我知道它仍然有点乱,但就像我说我是新来的