麻烦从函数回应

时间:2012-11-04 06:36:39

标签: php mysql pdo

所以我遇到一个变量从函数传递到页面函数被调用的问题如果我在函数本身写一个echo命令变量会回显很好,但当我从文件中回显我调用该函数它根本不会显示任何内容。以下是我的代码示例:

//CHECK LOGIN CREDINTIALS
function checkLogin($conn,$myusername, $mypassword, $count) {

       $stmt = $conn->prepare('SELECT COUNT(*) FROM `CLL_users` WHERE `user_name`= :userName AND `password`= :userPass');
       $stmt->bindValue(':userName', $myusername);
       $stmt->bindValue(':userPass', $mypassword);
       $stmt->execute();
       $count = $stmt->fetchColumn();
       echo "$count<br>";
}

这是我试图将变量从函数传递到

的文件
<?php
session_start();
$_SESSION['myusername'] = $myusername;
session_is_registered("myusername");
session_is_registered("mypassword");

require 'functions.php';
require 'DB.php';
ob_start();
// Define $myusername and $mypassword 
$myusername =$_POST['myusername']; 
$mypassword =md5($_POST['mypassword']);

checkLogin($conn, $myusername, $mypassword, $count);

echo $count;
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

    $myusername1 = $_SESSION['myusername'];
    //UPDATE LAST LOGIN IN DATABASE
    date_default_timezone_set('America/Chicago');
    $last_login = date('m/d/Y h:i:s a', time());
    lastLogin($conn,$myusername1,$last_login);
    header("location:form.php");


}
else {
echo $count;
echo "$mypassword<br>";
echo "$myusername<br>";
echo "Wrong Username or Password";

}
ob_end_flush();
?>

1 个答案:

答案 0 :(得分:1)

喜欢这个

function checkLogin($conn,$myusername, $mypassword) {

       $stmt = $conn->prepare('SELECT COUNT(*) FROM `CLL_users` WHERE `user_name`= :userName AND `password`= :userPass');
       $stmt->bindValue(':userName', $myusername);
       $stmt->bindValue(':userPass', $mypassword);
       $stmt->execute();
       $count = $stmt->fetchColumn();
       return $count;
}

并在您的代码中

$myusername =$_POST['myusername']; 
$mypassword =md5($_POST['mypassword']);
$count=checkLogin($conn,$myusername, $mypassword);
if($count==1){
  //your code
}