PHP MySQLi连接和查询错误处理.如何? [英] PHP MySQLi connection and query error handling. How?

查看:66
本文介绍了PHP MySQLi连接和查询错误处理.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下PHP代码有效.我只是似乎无法以自定义方式处理其错误.

The following PHP code works. I just don't seem to be able to handle its errors in a custom way.

例如,当我故意拼写错误的连接字符串中的任何内容以返回代码"3"以关闭数据库时,我的AJAX脚本将永远挂在beforeSend之前...

For example, when I intentionally misspell anything in the connection string to return code "3" for database down, my AJAX script just hangs in beforeSend forever...

这就是我得到的:

<?php  

  if(isset($_POST["postT_VAL"])) {

  $client_id    = $_POST["postCLIENT_ID"];
  $project_id   = $_POST["postPROJECT_ID"];
  $mainsheet_id = $_POST["postMAINSHEET_ID"];
  $field_name = $_POST["postT_ID"];
  $field_value = $_POST["postT_VAL"];

  $link = mysqli_connect("database.domain.com", "username1", "password1", "db220474");

  if (!$link) {

  /* return 3 = database offline */
  echo "3";

  } else {

  /* build query */
  $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  

  /* execute query */    
  mysqli_query($link, $sql);

  /* return 0 = no update / 1 = successful update */
  echo "".mysqli_affected_rows($link);

  /* close connection */
  mysqli_close($link);

 }

 }

?>

新研究

好的.经过一些研究,我发现这可行.看来您需要告诉mysqli引发异常..由于某些原因,这不同于仅尝试以"IF"方式处理错误.有什么改进建议吗?

Alright. After some research I have found that this works. It appears that you need to tell mysqli to throw exceptions.. For some reason this is different than just trying to handle an error the "IF" way. Any suggestions for improvement?

mysqli_report(MYSQLI_REPORT_STRICT);

      try {
           $link = mysqli_connect("database.domain.com", "username1", "password1", "db220474");
      } catch (Exception $e ) {
           echo "3";
           exit;
      }

代码更新

这是所有用户都可以通过的经过最终测试且可以正常使用的PHP解决方案.

<?php  

  /* Status Codes

     return 0 = Nothing to Update
     return 1 = Successful Update Query
     return 2 = Database Connection refused
     return 3 = MySQL Query Error OR Wrong URL Parameters */

  mysqli_report(MYSQLI_REPORT_OFF);

  if(isset($_GET["postT_VAL"])) {

  $client_id    = $_GET["postCLIENT_ID"];
  $project_id   = $_GET["postPROJECT_ID"];
  $mainsheet_id = $_GET["postMAINSHEET_ID"];
  $field_name = $_GET["postT_ID"];
  $field_value = $_GET["postT_VAL"];

  try {
       $link = mysqli_connect("domain", "username", "password", "database");
  } catch (Exception $e) {
       // echo "".$e->getCode();
       /* return 2 = Database Connection refused */
       echo "2";

       exit;
  }

  /* Build dynamic Update Query string */
  $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  

  /* Execute Update Query */    
  if(!mysqli_query($link, $sql)) {

  echo "3";
  /* Close Connection */
  mysqli_close($link);

  exit;

  } else {

  /* return 0 = Nothing to Update / 1 = Successful Update Query */
  echo "".mysqli_affected_rows($link);

  /* Close Connection */
  mysqli_close($link);

  }

 }

?>

推荐答案

以下是经过最终测试且可以正常使用的PHP解决方案,供所有人参考.

<?php  

  /* Status Codes

     return 0 = Nothing to Update
     return 1 = Successful Update Query
     return 2 = Database Connection refused
     return 3 = MySQL Query Error OR Wrong URL Parameters */

  mysqli_report(MYSQLI_REPORT_STRICT);

  if(isset($_GET["postT_VAL"])) {

  $client_id    = $_GET["postCLIENT_ID"];
  $project_id   = $_GET["postPROJECT_ID"];
  $mainsheet_id = $_GET["postMAINSHEET_ID"];
  $field_name = $_GET["postT_ID"];
  $field_value = $_GET["postT_VAL"];

  try {
       $link = mysqli_connect("domain", "username", "password", "database");
  } catch (Exception $e) {
       // echo "".$e->getCode();
       /* return 2 = Database Connection refused */
       echo "2";

       exit;
  }

  /* Build dynamic Update Query string */
  $sql = "UPDATE tbl_mainsheet2 SET ".$field_name." = '".$field_value."' WHERE client_id = '".$client_id."' AND project_id = '".$project_id."' AND mainsheet_id = '".$mainsheet_id."'";  

  /* Execute Update Query */    
  if(!mysqli_query($link, $sql)) {

  echo "3";
  /* Close Connection */
  mysqli_close($link);

  exit;

  } else {

  /* return 0 = Nothing to Update / 1 = Successful Update Query */
  echo "".mysqli_affected_rows($link);

  /* Close Connection */
  mysqli_close($link);

  }

 }

?>

这篇关于PHP MySQLi连接和查询错误处理.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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