从DB返回值。 PHP

时间:2014-11-20 15:51:39

标签: php pdo

在此脚本中,我试图检查用户是否存在。如果是,则显示它是否显示错误消息。问题是我只收到错误消息,因此猜测脚本没有正确返回任何内容。请好好看看。 感谢。

public function check_user($uid){
        $sql        =   "SELECT * from users WHERE uid= :uid ";
        $q = $this->db->prepare($sql);
        $q->execute(array(':uid'=>$uid));
        $result = $q->fetch(PDO::FETCH_ASSOC);
        return $result;
}

profile.php

    <?php
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);
    session_start();

    include_once('php/classes/db_config.php');
    include_once('php/classes/class.user.php');

    $user1 = new User($con);
    $is_loggedin = (isset($_SESSION['uid']));
    $is_uid = (!empty($_GET['uid']) && is_numeric($_GET['uid']));
    $def_uid = ($is_uid) ? $_GET['uid'] : $_SESSION['uid'];
    $user_valid = ($is_uid == true) ? $user1->check_user($def_uid) : 1;

    @$name_id = $_SESSION['user']['uid'];
    $name = $_SESSION['user']['uname'];
    $fullname = $_SESSION['user']['fullname'];
    $bio = $_SESSION['user']['bio'];
    $time = date("Y-m-d H:i:s");

    if (isset($_POST['logout'])) {
        session_destroy();
        header('Location: index.php');
        exit;
    }

    if (isset($_POST['area_sub'])) {
        if (empty($_POST['area'])) {
            echo "<script>alert('Empty area field.')</script>";
        }else{
            $uid = $_GET['uid'];
            if ($uid == '' || $uid == 0) {
                $uid = $name_id;
            }
            $user1->post($name_id, $uid, $name, $_POST['area'], $time);
        }
    }
    if($is_loggedin){
        $sql = "SELECT * FROM follow_req WHERE user_two_req= :user_two_req";
        $query = $con->prepare($sql);
        $query->execute(array( ':user_two_req' => $name_id));
        $result = $query->fetchALL(PDO::FETCH_ASSOC);
    }


?>

            // If user is valid
            if($user_valid == 1) {
                // User is logged in user
                if($def_uid == $name_id) {
                    include_once 'php/classes/profile_func.php';
                } 
                include_once 'php/classes/user_info.php';

            }else{?>
                <h2>No Such User Exists</h2>
                <h3>Please select a different user or <a href='index.php'>Login</a></h3>
                <?php if($is_loggedin == true){ ?>
                        <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                    <?php
                }
            } ?>

1 个答案:

答案 0 :(得分:1)

修正了它。

if($user_valid == 1) {
            // User is logged in user
            if($def_uid == $name_id) {
                include_once 'php/classes/profile_func.php';
            } 
            include_once 'php/classes/user_info.php';

        }else{?>
            <h2>No Such User Exists</h2>
            <h3>Please select a different user or <a href='index.php'>Login</a></h3>
            <?php if($is_loggedin == true){ ?>
                    <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                <?php
            }
        }

将其更改为:

--> if($user_valid == true) {
            // User is logged in user
            if($def_uid == $name_id) {
                include_once 'php/classes/profile_func.php';
            } 
            include_once 'php/classes/user_info.php';

        }else{?>
            <h2>No Such User Exists</h2>
            <h3>Please select a different user or <a href='index.php'>Login</a></h3>
            <?php if($is_loggedin == true){ ?>
                    <h3>Go Back to <a href="profile.php?uid=<?php echo $name_id;?>">My Profile</a></h3>
                <?php
            }
        }