无法从mysql数据库php回显数据

时间:2015-07-17 16:59:07

标签: php mysql echo

出于某种原因,我无法回复表格中的数据。

这是我连接数据库“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");
    ?>

    &raquo; <a href="main.php">Your stats</a>
    &raquo; <a href="rankings.php">Battle</a>
    &raquo; <a href="explore.php">Explore</a>
    &raquo; <a href="gym.php">GYM</a>
    &raquo; <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

    }

?>

由于某种原因,回声部分不起作用,我不明白为什么。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码之类的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...
?>