PHP 不是 'Syncing' 与 MySQL 数据库?
PHP Not 'Syncing' with MySQL DB?
我有一个包含用户信息的 mySQL 数据库。使用正常工作的用户注册系统将信息添加到数据库中。这是一个示例用户 (PHPmyadmin):
问题是我有一个基于 PHP 的 'my account' 页面,用于检索有关用户帐户的信息。当我从 PHPMyAdmin 更新用户信息时,甚至当我添加有关用户的新数据时,此信息似乎与 MySQL 不同步(例如,我的 PHP '检测新添加的 'RealName' 字段,尽管它识别出已经存在一段时间的 'EmailAddress' 字段。
PHP/HTML:
<div class="modal-body">
<img src="assets/user.jpg" style="border-radius:150px;height:150px;width:150px;float:left;"><br>
<h2 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;">  <?=$_SESSION['RealName']?></h2><br><br>
<h4 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;"><i>    @<?=$_SESSION['Username']?></i></h4><br><br>
<hr>
<a href=""><p class="desc" style="font-style: normal;float:left;">    Change Profile Image...</h2></a> <a href=""><p class="desc" style="font-style: normal;float:left;">    Change Your Name...</h2></a>
</div>
<div class="modal-footer-alt"><br><br>
<h4>Account Info  </h4>
<p class="desc" style="font-style: normal;float:left;"><b>E-Mail Address: </b><?=$_SESSION['EmailAddress']?>  </p> <code style="float:left;">Private</code><br><br>
<p class="desc" style="font-style: normal;float:left;"><b>Password: </b> ●●●●●●●●●●  </p> <code style="float:left;">Private</code><br>
<h4>Bio  </h4>
<textarea rows="4" cols="40"><?=$_SESSION['Bio']?></textarea>
<h4>More  </h4>
<a href="../account/die.php"><button class="btn btn-default">Log Out</button></a><a href="../account/die.php"><button class="btn btn-default">Sign Up for Contributor Rewards</button></a>
</div>
它在网页上的显示方式:
请注意,某些信息会出现,而其他信息不会出现。这不是我的 HTML 的问题,就像我用静态值替换 PHP 一样,它显示正常。
这是我用来启动会话的代码:
<?php
session_start();
$dbhost = "PRIVATE"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "PRIVATE"; // the name of the database that you are going to use for this project
$dbuser = "PRIVATE"; // the username that you created, or were given, to access your database
$dbpass = "PRIVATE"; // the password that you created, or were given, to access your database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
注销代码:
<?php include "base.php"; $_SESSION = array(); session_destroy(); ?>
您需要重新加载 $_SESSION
值...这可以通过创建页面 logout.php
并在其中包含函数 session_start(); session_destroy();
来实现。您的登录页面也需要更新以包含正确的会话数据。
我不同意你的工作流程,不会使用这种方法。在与用户相关的所有内容中,我只信任来自数据库(或身份验证机构,例如 OAuth 或 OpenId)的最新数据,从不存储会话数据。
每次我需要用户数据等关键信息时,我都会查询数据库。用户可能会在会话中间更改 his/her 名称,这应该由您的系统实时反映。
我也会避免OR DIE
。请考虑使用 try...catch
并处理错误。
try {
include ('ConnectionData.php'); // user, pass, db, localhost
$sqlGetId = "SELECT etcetcetc";
$sqlResultGetId = mysql_query($sqlGetId);
$rowGetId = mysql_fetch_array($sqlResultGetId);
if ( $rowGetId[0] > 0 ) {
// consume data
我有一个包含用户信息的 mySQL 数据库。使用正常工作的用户注册系统将信息添加到数据库中。这是一个示例用户 (PHPmyadmin):
问题是我有一个基于 PHP 的 'my account' 页面,用于检索有关用户帐户的信息。当我从 PHPMyAdmin 更新用户信息时,甚至当我添加有关用户的新数据时,此信息似乎与 MySQL 不同步(例如,我的 PHP '检测新添加的 'RealName' 字段,尽管它识别出已经存在一段时间的 'EmailAddress' 字段。
PHP/HTML:
<div class="modal-body">
<img src="assets/user.jpg" style="border-radius:150px;height:150px;width:150px;float:left;"><br>
<h2 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;">  <?=$_SESSION['RealName']?></h2><br><br>
<h4 class="desc" style="font-style: normal;padding:0px;margin:0px;float:left;"><i>    @<?=$_SESSION['Username']?></i></h4><br><br>
<hr>
<a href=""><p class="desc" style="font-style: normal;float:left;">    Change Profile Image...</h2></a> <a href=""><p class="desc" style="font-style: normal;float:left;">    Change Your Name...</h2></a>
</div>
<div class="modal-footer-alt"><br><br>
<h4>Account Info  </h4>
<p class="desc" style="font-style: normal;float:left;"><b>E-Mail Address: </b><?=$_SESSION['EmailAddress']?>  </p> <code style="float:left;">Private</code><br><br>
<p class="desc" style="font-style: normal;float:left;"><b>Password: </b> ●●●●●●●●●●  </p> <code style="float:left;">Private</code><br>
<h4>Bio  </h4>
<textarea rows="4" cols="40"><?=$_SESSION['Bio']?></textarea>
<h4>More  </h4>
<a href="../account/die.php"><button class="btn btn-default">Log Out</button></a><a href="../account/die.php"><button class="btn btn-default">Sign Up for Contributor Rewards</button></a>
</div>
它在网页上的显示方式:
这是我用来启动会话的代码:
<?php
session_start();
$dbhost = "PRIVATE"; // this will ususally be 'localhost', but can sometimes differ
$dbname = "PRIVATE"; // the name of the database that you are going to use for this project
$dbuser = "PRIVATE"; // the username that you created, or were given, to access your database
$dbpass = "PRIVATE"; // the password that you created, or were given, to access your database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
注销代码:
<?php include "base.php"; $_SESSION = array(); session_destroy(); ?>
您需要重新加载 $_SESSION
值...这可以通过创建页面 logout.php
并在其中包含函数 session_start(); session_destroy();
来实现。您的登录页面也需要更新以包含正确的会话数据。
我不同意你的工作流程,不会使用这种方法。在与用户相关的所有内容中,我只信任来自数据库(或身份验证机构,例如 OAuth 或 OpenId)的最新数据,从不存储会话数据。
每次我需要用户数据等关键信息时,我都会查询数据库。用户可能会在会话中间更改 his/her 名称,这应该由您的系统实时反映。
我也会避免OR DIE
。请考虑使用 try...catch
并处理错误。
try {
include ('ConnectionData.php'); // user, pass, db, localhost
$sqlGetId = "SELECT etcetcetc";
$sqlResultGetId = mysql_query($sqlGetId);
$rowGetId = mysql_fetch_array($sqlResultGetId);
if ( $rowGetId[0] > 0 ) {
// consume data