如何声明全局变量

时间:2017-10-18 00:59:41

标签: php variables

我曾经能够做到这一点,但现在它无法正常工作。 我不确定它是否导致此更改的PHP版本。 我了解includerequire将能够执行此操作,但在我尝试使用它时会导致很多问题。我很感激任何输入,因为PHP不是我最好的理解。

file1.php

 <?php
 include "functions.php"
  $db = new Functions();

 if ($db->isUserExisted($accountemail)) {
        // user is already existed - error response
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed";
        error_log(print_r($response,true));
        echo json_encode($response);
  echo "weblogin-after checking existed"."<br>";

       }

的functions.php

<?php
 class Functions {
require "./config.php";//connecting to database
 public function isUserExisted($email) {
 global $con;

   echo"checking email";
    $result = "SELECT * from db WHERE Account_email = :email";

    $stmt = $con->prepare($result);
   $stmt->bindParam(':email', $email, PDO::PARAM_STR); 
    $stmt->execute();
    $row =$stmt->fetch(PDO::FETCH_ASSOC);
   $no_of_rows =$stmt->rowCount();
   echo $no_of_rows;

    if ($no_of_rows > 0) {
        // user existed 
        return true;
    } else {
    error_log(print_r('user not existed',true));
        // user not existed
        return false;
     }
  }
 }

现在它不会移动$db = new Functions();
没有PHP错误

的config.php

<?php
  /**
   * DB uration variables
   */
error_reporting(E_ALL);
$DB_HOST= "localhost";
$DB_USER="user";
$DB_PASSWORD= "****";
$DB_DATABASE="db";


try {

$con = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_DATABASE.";", $DB_USER, $DB_PASSWORD);
   $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  error_log(print_r('config',true));

  echo "Connected successfully "; 
  return $con;
  error_log('connect',true);
   } catch (PDOException $e) {
  error_log(print_r( "Error!: " . $e->getMessage()."<br/>" ,true)) ;
   error_log(print_r('not connected',true));
  die();
  }
  ?>

1 个答案:

答案 0 :(得分:0)

您可以先尝试启用PHP错误报告。 您可以在file1.php

之后的<?php中添加以下代码

ini_set('display_startup_errors', 1); ini_set('display_errors', 1); error_reporting(-1);