PHP MySQLi连接和查询错误处理。怎么样?

时间:2017-08-03 03:09:38

标签: php mysql mysqli

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

例如,当我故意拼错连接字符串中的任何内容以返回数据库中的代码“3”时,我的AJAX脚本只会在beforeSend中挂起...

这就是我得到的:

<?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”方式处理错误。有任何改进建议吗?

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);

  }

 }

?>

4 个答案:

答案 0 :(得分:1)

这是最终经过测试和运行的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);

  }

 }

?>

答案 1 :(得分:0)

执行查询时尝试此操作

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

答案 2 :(得分:0)

enter code here
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 // Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
{
echo("Error description: " . mysqli_error($con));
}
 mysqli_close($con);
?>

试试这个好运

答案 3 :(得分:0)

你可以这样做:

try {
   $link = mysqli_connect("domain", "username", "password","database");
 } catch (Exception $e) {
       echo $e;
   exit;
 }