我是PHP的初学者,我正在使用session和pg函数。所以我想从数据库(基本上是用户的详细信息)返回用户的名字和姓氏,并使用session将其显示到不同的页面。并且它使用pg_fetch_assoc()返回,但问题是它在每个值之后显示1,如(firstname 1),(lastname 1)。我该如何解决呢?有没有其他方法可以从数据库返回值并显示它,并将这些值用作条件。
由于
以下是我的代码:
$login = trim($_POST['login']);
$pass = trim($_POST['pass']);
$result = pg_execute($conn, "login_query", array($login, hash(HASH_ALGO, $pass)));
$records = pg_num_rows($result);
if($records == 1){
$_SESSION['user_id'] = $login;
$row = pg_fetch_assoc($result);
$_SESSION['first_name'] = $row['first_name'];
$_SESSION['last_name'] = $row['last_name'];
$_SESSION['last_access'] = $row['last_access'];
$_SESSION['user_type'] = $row['user_type'];
//$_SESSION['details'] = $row;
pg_execute($conn, "update_query", array($login));
/* $_SESSION['output'] = $output();*/
$_SESSION['output'] = "Welcome back " . pg_fetch_result($result,0,"first_name") . pg_fetch_result($result,0,"last_name").'</br>';
$_SESSION['output'] .= "Our records show that your </br>" . "email address is " . pg_fetch_result($result,0,"email_address") . "</br>";
$_SESSION['output'] .= "and you last accessed our system: " . pg_fetch_result($result,0,"last_access") . "</br>";
header('location: ./user-dashboard.php');
}
这里是var_dump($row);
值:
array(9) { ["user_id"]=> string(20) "jdoe " ["password"]=> string(32) "179ad45c6ce2cb97cf1029e212046e81" ["user_type"]=> string(2) "c " ["email_address"]=> string(256) "jdoe@gmail.com " ["first_name"]=> string(128) "John " ["last_name"]=> string(128) "Doe " ["birth_date"]=> string(10) "1998-02-05" ["enrol_date"]=> string(10) "2017-01-01" ["last_access"]=> string(10) "2017-10-18" }
答案 0 :(得分:0)
pg_fetch_assoc
返回值OK。您可以在var_dump($row);
的结果中看到它 - 请参阅["last_name"]=> string(128) "Doe "
- 它有额外的空间 - 是的,但不是 1 。因此,另一部分代码在$row["last_name"]
值之后添加 1 。
答案 1 :(得分:0)
<h1 class="floating-box" ><?php echo print_r($_SESSION['first_name'],True); ?></h1>
<h1 class="floating-box" ><?php echo print_r($_SESSION['last_name']); ?></h1>
<h1 class="floating-box" ><?php echo print_r($_SESSION['last_access']); ?></h1>
抱歉,我忘了在print_r()函数中添加true。谢谢你的帮助。 @Vao Tsun