怎么做sql检查?

时间:2015-08-20 04:56:21

标签: php sql

Sub makeSubForders(ByVal fieldName As String)
    Const MDL       As String = "\2_To_TR\"
    Const D_PATH    As String = "D:\_Project\_Term\_Mobile\Mobile_Common_Term_130115_"
    Const TERMS     As String = "4_Term\Mobile_Common_Term_130115_"
    Const EXT       As String = ".xlsx"
    Dim pathIni As String, pathDiv As String
    Dim srceFile As String, destFile As String, mainPath As String
    mainPath = ActiveSheet.Cells(1, 8).Value2
    If Len(Dir(mainPath, vbDirectory)) = 0 Then
        MkDir mainPath: MkDir mainPath & "\" & MDL
    End If
    pathIni = mainPath & MDL & fieldName
    If Len(Dir(pathIni, vbDirectory)) = 0 Then
        MkDir pathIni: pathIni = pathIni & "\"
        MkDir pathIni & "_Query"
        MkDir pathIni & "2_File"
        MkDir pathIni & "3_INI"
        MkDir pathIni & "4_Term"
        MkDir pathIni & "5_Reference"
        MkDir pathIni & "6_TM"
        MkDir pathIni & "7_Log"
        MkDir pathIni & "8_PO"
        MkDir pathIni & "6_To_Client\"
        MkDir pathIni & "6_To_Client\" & fieldName
    End If
    pathDiv = ActiveSheet.Cells(8, 5).Value     '"Mobile" or "BOX"
    Select Case pathDiv
        Case "Mobile"
            If Len(Dir(pathIni & "\Mobile_Path", vbDirectory)) = 0 Then
                MkDir pathIni & "\Mobile_Path"
            End If
        Case "BOX"
            If Len(Dir(pathIni & "\BOX_Path", vbDirectory)) = 0 Then
                MkDir pathIni & "\BOX_Path"
            End If
    End Select
End Sub

您好,我只是想问为什么sql语句没有读取第一个语句,即如果有相同的用户名和s_id,则用户应该更改它。但它会打印成功插入到数据库中,但数据库中没有数据,因为用户名或s_id已经存在于数据库中。是否有语法错误?请帮帮我

5 个答案:

答案 0 :(得分:3)

使用mysql_num_rows来计算/比较行。 ($result) > 0($result) == 1

更好

改变
if (($result) == 1) {

if(mysql_num_rows($result) > 0) {

警告:自PHP 5.5.0起,不推荐使用MYSQL扩展,自PHP 7.0.0起,该扩展已被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 1 :(得分:1)

非常好的问题,人们总是对这个问题感到困惑。首先,跳过代码。问自己,SELECT语句返回什么?如果成功,则返回一个表。

现在,告诉我你可以比较一个值为1的表吗?答案是否定的,你不是。您要做的是首先计算表的行数。并且由于您的userId是唯一的,您将始终找到一行(如果匹配)或零行(如果不匹配)。

因此,请检查该行数是否为1。怎么做? 像这样更改你的代码,

      $sql = "SELECT *
            FROM users
            WHERE username = '$username' AND s_id = '$s_id'";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) == 1) {
    $errorMessage = 'Username or Student ID already taken. Choose another one'; 
    } else {            
    $sql   = "INSERT INTO users VALUES ('$username','$password','$f_name','$l_name','$s_id','$email')";
    mysql_query($sql);
    $errorMessage = 'Registration is Successful. You can <a href="userpage.php">Login</a> Now.';
    }

如前面的答案所示,

自PHP 5.5.0起,不推荐使用MYSQL扩展,自PHP 7.0.0起,该扩展已被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

答案 2 :(得分:0)

答案 3 :(得分:0)

当您使用SELECT count(*)FROM ...并检查结果(使用mysql_result函数)是否大于0时,您可以提高性能:

$sql = "SELECT count(*)
    FROM users
    WHERE username = '$username'
    AND s_id = '$s_id'";
$result = mysql_query($sql);
if (mysql_result($result, 0, 1) > 0) {
    $errorMessage = 'Username or Student ID already taken. Choose another one'; 
} else {            
    $sql = "INSERT INTO users VALUES ('$username','$password','$f_name','$l_name','$s_id','$email')";
    if (mysql_query($sql)) {
        $errorMessage = 'Registration is Successful. You can <a href="userpage.php">Login</a> Now.';
    } else {
        $errorMessage = 'An error occurred.';
    }
}

答案 4 :(得分:0)

您的学生表ID应该是主键 - 因此无需检查姓名

$pdo = 'mysql:host=' . $host . ';dbname=' . $db;
$dbL = new PDO($pdo, $user, $pass);
$sql = 'SELECT 1 FROM users WHERE s_id = '. ceil($s_id);
$sth = $dbL->prepare($sql);
$sth->execute();
$row = $sth->fetch(PDO::FETCH_NUM);
if ($row[0]) {
  //already exists
} else {
  //new record and save it
}