2013-06-28 75 views
1

我又遇到了php和mysql的問題。我有一個與表用戶的數據庫設置,我想要一個SELECT COUNT(*) FROM users WHERE {value1} {value2}等......但問題是,我想比較的3個字段不是在表中順序,並在嘗試SELECT查詢時,結果可vairable( $結果)不正確返回(!$結果)。有沒有辦法檢查一個mysql表中有多個字段的多個字段?下面是我想要實現的一個示例: 稱爲users的mysql表包含以下字段:a,b,c,d,e,f,g,h,i,j,k,l and m。 我想製作一個SELECT COUNT(*) FROM用戶WHERE a='$_SESSION[user]' and d='$_SESSION[actcode]' and j='$_SESSION[email]'但引號中的語句是我的查詢,它始終執行if (!$result) { error("An error has occurred in processing your request.");}語句。我究竟做錯了什麼?相反,每當我嘗試使用只有一個字段的聲明,例如,代碼工作正常!這是一個令人討厭的問題,我似乎無法解決!我已經發布了下面的代碼,還要注意錯誤函數是我所做的一個自定義函數,並且正常工作。MYSQL表挑剔關於字段?

<?php 
include "includefunctions.php"; 
$result = dbConnect("program"); 
if (!$result){ 
    error("The database is unable to process your request at this time. Please try again later."); 

} else { 

ob_start(); 
session_start(); 
if (empty($_SESSION['user']) or empty($_SESSION['password']) or empty($_SESSION['activationcode']) or empty($_SESSION['email'])){ 
    error("This information is either corrupted or was not submited through the proper protocol. Please check the link and try again!"); 
} elseif ($_SESSION['password'] != "password"){ 
    error("This information is either corrupted or was not submited through the proper protocol. Please check the link and try again!"); 
} else { 

    $sql = "SELECT * FROM `users` WHERE `username`='$_SESSION[user]' and `activationcode`='$_SESSION[activationcode]' and `email`='$_SESSION[email]'";/*DOES NOT MATTER WHAT ORDER THESE ARE IN, IT STILL DOES NOT WORK!*/ 
    $result = mysql_query($sql); 
    if (!$result) { 
     error("A database error has occurred in processing your request. Please try again in a few moments.");/*THIS IS THE ERROR THAT WONT GO AWAY!*/ 
    } elseif (mysql_result($result,0,0)==1){/*MUST EQUAL 1 OR ACCOUNT IS INVALID!*/ 
     echo "Acount activated!"; 
    } else { 
      error("Account not activated.");  
    } 
} 
} 
ob_end_flush(); 
session_destroy(); 
?> 
+1

嘗試mysql_error尋找查詢被執行後,立即 - 那會告訴你什麼是MySQl認爲是錯誤的。你的條件的順序應該不重要。 – 2013-06-28 04:46:19

+0

訂購劑量物質。還有其他的錯誤。 –

+0

我試了兩個答案,他們都沒有工作。但是我在「'」字符中看到了一個問題,因爲雙引號是正確的,但由於某種原因,電子郵件中的鏈接在它後面有一個額外的單引號。但是引號是封裝字符串所必需的。另外,如果在沒有引用的情況下運行,它仍然無法正常運行。 GRRRRR這個問題使我感到憤怒,爲什麼它不會工作! –

回答

0

嘗試在大括號{}封閉的$_SESSION變量,並添加or die(mysql_error())到您的查詢的末尾 -

$sql = "SELECT * FROM `users` WHERE `username`='{$_SESSION['user']}' and `activationcode`='{$_SESSION['activationcode']}' and `email`='{$_SESSION['email']}'";/*DOES NOT MATTER WHAT ORDER THESE ARE IN, IT STILL DOES NOT WORK!*/ 
$result = mysql_query($sql) or die(mysql_error()); 
0

store your session value in another varibles then make query , i think it's work proper

$usr=$_SESSION['user']; 
$acod=$_SESSION['activationcode']; 
$eml=$_SESSION['email']; 
$sql = "SELECT * FROM `users` WHERE `username`='$usr' and `activationcode`='$acod' and `email`='$eml'"; 
$result = mysql_query($sql) or die(mysql_error());