1

我又遇到了 php 和 mysql 的问题。我有一个与表用户一起设置的数据库,我想创建一个SELECT COUNT(*) FROM users WHERE {value1} {value2}等...但问题是我要比较的 3 个字段在表中不是按顺序排列的,并且在尝试 SELECT 查询时,结果可变($结果)未正确返回(!$result)。有没有办法检查 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.");}陈述。我究竟做错了什么?相反,每当我尝试仅使用一个字段(例如 a)的语句时,代码都可以正常工作!这是一个我似乎无法解决的烦人问题!我已经发布了下面的代码,还要注意错误函数是我制作的自定义函数,并且工作正常。

<?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();
?>
4

2 回答 2

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());
于 2013-06-28T04:54:47.147 回答
0

将您的会话值存储在另一个变量中然后进行查询,我认为它工作正常

$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());
于 2013-06-28T05:29:38.687 回答