PHP类和未捕获的PDOException错误

时间:2017-12-11 18:06:05

标签: php mysql

我已经远离PHP好几年并使用Classes编写应用程序等...但是,我得到了以下堆栈错误并且有点丢失到我错误的地方或多或少从另一堂课所以不确定我错过了什么......

错误:

  

致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或   访问冲突:1064您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在第1行''附近使用的语法   /home/ambcom/public_html/tracking/login/class/mailtranslations.php:17   堆栈跟踪:#0   /home/ambcom/public_html/tracking/login/class/mailtranslations.php(17):   PDOStatement-> execute()#1   /home/ambcom/public_html/tracking/admin/emailtranslations.php(11):   MailTranslations-> __ construct()#2 {main}引入   /home/ambcom/public_html/tracking/login/class/mailtranslations.php on   第17行

dbconfig.php文件

$tbl_prefix = ""; //Prefix for all database tables

$tbl_members = $tbl_prefix."members";
$tbl_memberinfo = $tbl_prefix."member_info";
$tbl_admins = $tbl_prefix."admins";
$tbl_attempts = $tbl_prefix."login_attempts";
$tbl_deleted = $tbl_prefix."deleted_members";
$tbl_tokens = $tbl_prefix."tokens";
$tbl_cookies = $tbl_prefix."cookies";
$tbl_appConfig = $tbl_prefix."app_config";
$tbl_mailLog = $tbl_prefix."mail_log";
$tbl_mailLog = $tbl_prefix."sms_log";
$tbl_mailTranslations = $tbl_prefix."mail_translations";
$tbl_smsTranslations = $tbl_prefix."sms_translations";

/login/class/mailtranslations.php

<?php
/**
* Handles application configuration settings stored in database `mail_translations` table
**/
class MailTranslations extends DbConn
{
  /**
  * Primarily instantiated in `login/partials/pagehead.php`. Meant to be instantiated once to minimize unnecessary database calls.
  * In any page where `pagehead.php` is included, settings can be pulled as such: `$this->setting_name` where `setting_name` corresponds to "setting" entry in `app_config` database table.
  **/
  function __construct() {

      parent::__construct();

      $sql = "SELECT setting, value FROM ".$this->tbl_mailTranslations;
      $stmt = $this->conn->prepare($sql);
      $stmt->execute();
      $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

      //Pulls all properties from database
      foreach ($settings as $key => $value) {

          $this->{$key} = $value;
      }

      $this->signin_url = $settings['base_url'].'/login';

      if ($this->from_email == '') {
          $this->from_email = $this->admin_email;
      }

  }
  /**
  * Pulls single translation statically from database without invoking new MailTranslations object. Meant to be used in pages where `pagehead.php` is not included.
  * Calls can be made like so: MailTranslations::pullTranslation('setting_name', 'db_var_type', 'lang')
  *
  * `setting_name` corresponds to "setting" entry in `mail_translations` table, and `db_var_type` should be desired db type such as `unsigned` for integers, `varchar`, etc.
  **/
  public static function pullTranslation($setting, $type = 'varchar', $lang = 'English')
  {

      $db = new DbConn;
      try {
      if ($type === 'varchar') {
          $sql = "SELECT value FROM ".$db->tbl_mailTranslations." WHERE setting = :setting LIMIT 1";
      } else {
          $sql = "SELECT CAST(value AS ".$type.") FROM ".$db->tbl_mailTranslations." WHERE setting = :setting AND lang = :lang LIMIT 1";
      }
          $stmt = $db->conn->prepare($sql);
          $stmt->bindParam(':setting', $setting);
          $stmt->bindParam(':lang', $lang);
          $stmt->execute();
          $result = $stmt->fetch(PDO::FETCH_NUM);

      } catch (PDOException $e) {

          $result[0] = "Error: " . $e->getMessage();
      }

      unset($db);

      return $result[0];

  }
  /**
  * Updates array of settings.
  * Calls can be made like so: $obj->updateMultiMailTranslations(array("setting1"=>"value1", "setting2"=>"value2", "etc"=>"etc"), "lang")
  **/
  public function updateMultiMailTranslations($settingArray, $lang = "English")
  {
      try {
          foreach ($settingArray as $setting=>$value) {

              try {

                  $sql = "UPDATE ".$this->tbl_mailTranslations." SET value = :value WHERE setting = :setting AND lang = :lang";

                  $stmt = $this->conn->prepare($sql);
                  $stmt->bindParam(":value", $value);
                  $stmt->bindParam(":setting", $setting);
                  $stmt->bindParam(":lang", $lang);
                  $stmt->execute();

              } catch (PDOException $e) {
                  $result['status'] = false;
                  $result['message'] = "Error: " . $e->getMessage();
              }
          }

          $result['status'] = true;
          $result['message'] = "<div class=\"alert alert-success alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Changes Saved Successfully</div>";

      } catch (Exception $x) {
          $result['status'] = "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>Your account has been created, but you cannot log in until it has been verified</div>";
          $result['message'] = $x->getMessage();

      }

      return $result;
  }
  /**
  * Pulls all mail translations statically from database with descriptions, lang, and input types. Meant to be used specifically in `admin/editconfig.php` page.
  * Calls can be made like so: Translations::pullAllMailTranslations()
  **/
  public static function pullAllMailTranslations()
  {
      if ($_SESSION["admin"] == 1) {

          $db = new DbConn;

          try {
          $sql = "SELECT setting, value, description, type, lang FROM ".$db->tbl_mailTranslations." where type != 'hidden' order by -sortorder desc";

          $stmt = $db->conn->prepare($sql);
          $stmt->execute();
          $result = $stmt->fetchAll(PDO::FETCH_NUM);

          } catch (PDOException $e) {

              $result = "Error: " . $e->getMessage();
          }

      } else {
          $result = "You must be an admin to use this method";
      }

      return $result;
  }

}

admin / emailtranslations.php文件

  

第11行:$ conf =新的MailTranslations;

<?php
/**
* Configuration edit page
*
* Tags correspond to setting name, hover over them for setting description
**/
$pagetype = 'superadminpage';
$title = 'Email Translations';
require '../login/partials/pagehead.php';

$conf = new MailTranslations;
$settingsArr = $conf->pullAllMailTranslations();

//Gets langs from settings array
foreach ($settingsArr as $key => $value) {
 $groupedArr[$value[4]][] = $value;
}

?>

0 个答案:

没有答案