由于某种原因,我无法从表中回显数据。
这是我连接到数据库“functions.php”的地方
<?php
function connect() {
mysql_connect("localhost","username","password");
mysql_select_db("DBname");
}
function protect($string) {
return mysql_real_escape_string(strip_tags(addslashes($string)));
}
?>
然后我将其包含在我的标题中
<?php
include("functions.php");
Connect();
?>
<?php
if(isset($_SESSION['uid'])) {
include("safe.php");
?>
» <a href="main.php">Your stats</a>
» <a href="rankings.php">Battle</a>
» <a href="explore.php">Explore</a>
» <a href="gym.php">GYM</a>
» <a href="logout.php">Logout</a>
<?php
} else {
?>
<form action="login.php" method="POST">
Username <input type="text" name="Username">
Password <input type="password" name="Password">
<input type="submit" name="login" value="login">
</form>
<?php
}
?>
这是我的 main.php,我试图在其中回显数据。
<?php
session_start();
include ("header.php");
if(!isset($_SESSION['uid'])) {
echo "You must be logged in to view this page";
} else {
?>
<center><h1>Your stats</h1></center>
<br /> <br />
<table cellpadding="3" cellspacing="5">
<tr>
<td>Username:</td>
<td><i><?php echo $User['Username']; ?></i></td>
</tr>
<tr>
<td>Level:</td>
<td><i><?php echo $Stats['Level']; ?></i></td>
</tr>
<tr>
<td>Attack:</td>
<td><i><?php echo $Stats['Attack']; ?></i></td>
</tr>
<tr>
<td>Defence:</td>
<td><i><?php echo $Stats['Defence']; ?></i></td>
</tr>
<tr>
<td>Strength:</td>
<td><i><?php echo $Stats['Strength']; ?></i></td>
</tr>
<tr>
<td>Will:</td>
<td><i><?php echo $Stats['Will']; ?></i></td>
</tr>
<tr>
<td>Cash:</td>
<td><i><?php echo $Stats['Cash']; ?></i></td>
</tr>
</table>
<?php
}
?>
由于某种原因, echo 部分无法工作,我不知道为什么。
最佳答案
尝试使用 PDO(如以下代码)连接数据库:
<?php
try
{
$pdo=new PDO('mysql:host=localhostOrHostName;dbname=YourDatabaseName','YourUserName','YourPassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
然后您可以使用上面的连接从数据库查询数据,如下所示:
<?php
$ab = "SELECT * FROM users WHERE id='".$_SESSION['uid']."'";
$rs=$pdo->prepare($ab);
$rs->execute();
$result = $rs->fetch(PDO::FETCH_ASSOC);
?>
可以按如下方式访问在 $result 中获取的结果
<?php
echo $result['column_name1'];
echo $result['column_name2']; // and so on...
?>
关于php - 无法从 mysql 数据库 php 回显数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31480999/