我使用PHP和MYSQL
我尝试catch无法捕获mysql语法错误,如下所示的sql
选择*从lsht_admin中,AIC ='12345'并且APAS ='abc'和RST = 1
我故意输入{=TEXTJOIN(",";TRUE;IF($A$1:$A$9=$F3;IF($B$1:$B$9=G$2;$C$1:$C$9;"");""))}
,后面加一个撇号。我的代码如下
12345'
它无法捕获错误并转到错误页面,仅向我显示下面的错误
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本以使用正确的语法 在第1行的'','2019-01-17 18:17:20')'附近
SOS:
1)如何捕获语法错误
2)或任何更好的书写方式
谢谢
下面的完整代码
try{
$sql = "SELECT * FROM lsht_admin WHERE AIC = '12345'' AND APAS = 'abc' AND RST = 1";
$run = mysql_query($sql);
if( $run === false ){
throw new Exception("404|$errMsg|$syRDT");
}else{
//continue
}
}catch(Exception $e){
//redirect to error page
}
<?php
session_start();
include "../inc/ondb.php";
include "../inc/func.php";
include "../inc/datetime.php";
//foreach ($_POST as $K => $S) { echo "<br/>$K : $S"; }
$btn = ( (isset($_POST["BTN"])&&($_POST!=""))?$_POST["BTN"]:"");
$btn = ($btn==""?$_GET["BTN"]:$btn);
$goto="Location: ../index.php?err=0";
switch ($btn){
case "index_Submit":
try{
$AIC = $_POST["AIC"]; $APAS = $_POST["APAS"];
$sql = "SELECT * FROM lsht_admin WHERE AIC = '$AIC' AND APAS = '$APAS' AND RST = 1"; //echo $sql;
$run = mysql_query($sql);
if( $run === false ){
$errMsg = "身份证/密码可能存在乱码,无法进入系统!";
$HID = logHis("4018","$errMsg",$_POST["AIC"],$syRDT);
throw new Exception("4018.$HID|$errMsg|$syRDT");
echo "asdfadsf";
}else{
$HID = logHis("1001",$_POST["AIC"]."成功进入系统",$_POST["AIC"],$syRDT);
$rs = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_assoc($run)){
$_SESSION["ARR"] = $row;
mysql_query("update lsht_admin set RDT = '$syRDT' WHERE AID = $row[AID]")or die(mysql_error());
switch($row["ATYP"]){
case 0: $goto="Location: ../blank.php"; break;
case 1:
$sql = "SELECT PAIC FROM lsht_admin WHERE ATYP=2 GROUP BY PAIC";
$rw = mysql_query($sql);
$nr = mysql_num_rows($rw);
if($nr==1){
$goto="Location: ../admin.php";
}else{
$goto="Location: ../index.php?err=1";
}
break;
case 2: $goto="Location: ../president.php"; break;
default : $goto="Location: ../blank.php"; break;
}
}
}
}catch(Exception $e){
$goto="Location: ../index.php?err=0";
}
break;
}
include "../inc/offdb.php";
header($goto);
?>