需要一些有关 PHP/mysql 错误处理的建议 [英] Need some advice on error handling in PHP / mysql

查看:56
本文介绍了需要一些有关 PHP/mysql 错误处理的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定在向最终用户提供 READABLE 反馈时最好使用哪种方法.我已经阅读了一些论坛,但并没有真正变得更明智(或者我还没有理解)

I'm not sure which method is best to use when giving READABLE feedback to end user. I've read some forums, but not really gotten any wiser (or I have not understood it)

我想在插入/更新失败、成功以及提供自定义反馈(例如检查项目是否已存在)时提供反馈.

I would like to give feedback when insertion / update fails, when it's a success and when giving custom feedback (like checking if an item alread exists).

对于 INSERT、UPDATE、DELETE、DROP 等,查询返回 TRUE 或 FALSE.因此,我的结果属性 $this->query_result 应始终为 true 或 false.

On INSERT, UPDATE, DELETE, DROP etc., the query returns either TRUE or FALSE. Therefor my result property $this->query_result should always be either true or false.

我的问题:

  • 动态地向用户显示反馈表单提交后(提交到相同页)
  • $this->query_result 为真返回一个字符串

我添加了代码来查看我在做什么(做错了)

I have added code to see what I'm doing (doing wrong)

这些是我用来连接/查询数据库的函数:

These are the functions I use for connecting / querying the DB:

  public function connect() 
  { 

      if (!($this->conn = mysql_connect($this->host, $this->username, $this->pwd)))  {
         die("Error connecting to DB by user = " . $this->username); 
      } 

      $this->db = mysql_select_db($this->dbname,$this->conn) 
        or die("Unable to connect to database " . $this->dbname); 
  }  

  private function query($sql) 
  {
      $this->query_result = mysql_query($sql, $this->conn)or die("Unable to query local database <b>". mysql_error()."</b><br>$sql"); 

      if (!$this->query_result){ 
          die("database query failed."); 
      } else { 
          return $this->query_result; 
      }
  } 

这是我的问题:我正在提供有关数据访问层 (DAL) 的反馈,请参见例如这个:

Here is my problem: I'm giving feedback on the Data Access Layer (DAL), see e.g. this:

  public function addNewPerson($formData)
  {
    $sql = "INSERT INTO my_table(`name`, `email`, `www`)";

    $sql .= " VALUES('".
      $formData['name']."','".
      $formData['email']."','".
      $formData['www']."','");

   $this->query($sql);
   return $this->query_result;
  }

通过返回一个文本字符串,返回结果将始终为真.从我读到的内容来看,我可能应该有一个处理错误/反馈的函数.

By returning a text string, the return result will always be true. From what I read, I should probably have a function which handles errors / feedback.

这是我目前在模板中使用反馈所做的:

This is what I'm currently doing with feedback in my template:

  if (isset($_POST['form_submit']))
  {

    if (isset($_POST['person_submit'])) {
      $formData = $sl->getFormData();
      $result = $myDB->addNewPerson($formData);

      if ($result == true)
      {
        echo '<script type="text/javascript" language="JavaScript">
              jQuery("#contentArea .messageWindow1").show(500);
              jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow1").hide(500); });
        </script>';
      } else {
        echo '<script type="text/javascript" language="JavaScript">
              jQuery("#contentArea .messageWindow2").show(500);
              jQuery("#contentArea :input").click(function(){ jQuery("#contentArea .messageWindow2").hide(500); });
        </script>';
      }
    }
  } 

<div id="contentArea">   
  <div class="messageWindow1"> <span class="msg"><?php echo $labelResult ?></span></div>
  <div class="messageWindow2"> <span class="msg"><?php echo $labelResult ?></span></div>
</div>

推荐答案

我会使用 PHP5 的内置异常处理来捕获错误和可能的验证错误.例如:

class DatabaseException extends Exception {} class ValidatorException extends Exception {} public function connect() { if (!($this->conn = mysql_connect($this->host, $this->username, $this->pwd))) { throw new DatabaseException("Error connecting to DB by user = " . $this->username); } if(!($this->db = mysql_select_db($this->dbname,$this->conn))) { throw new DatabaseException("Unable to connect to database " . $this->dbname); } } //.... public function addNewPerson($formData) { $sql = "INSERT INTO my_table(`name`, `email`, `www`)"; $sql .= " VALUES('". $formData['name']."','". $formData['email']."','". $formData['www']."','"); //If less than 2 characters, do not insert data. if (strlen($formData['name']) < 2) throw new ValidatorException( "Person not saved. Name field was to short or empty."); //If person already exists if($this->isPersonInList($formData['name'])) throw new ValidatorException( "Person already exists!"); //Process query $this->query($sql); return $this->query_result; }

In the invocation script

在调用脚本中

Couple of other things to point out with your script.

需要在脚本中指出的其他几件事.

  1. It's better to use PDO and prepared statements.
  2. You can also use the following to determine if the string length is met.
  1. 最好使用 PDO 并准备好声明.
  2. 还可以通过以下方式判断字符串长度是否为遇见了.

<前>$arr = 'Shoan';var_dump(isset($arr[10]));//错误的var_dump(isset($arr[2]));//真的

$arr = 'Shoan'; var_dump(isset($arr[10])); //false var_dump(isset($arr[2])); //true

  1. 过滤sql的输入之前的注入/XSS 攻击将其推入您的数据库或在您的应用程序中使用它.

这篇关于需要一些有关 PHP/mysql 错误处理的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆