我再次遇到php和mysql的问题。我有一个表用户的数据库设置,我想创建一个SELECT COUNT(*) FROM users WHERE {value1} {value2}
等...但问题是我要比较的3个字段在表中没有按顺序排列,当尝试SELECT查询时,结果vairable($ result)未正确返回(!$ 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.");}
语句。我究竟做错了什么?相反,每当我尝试使用只有一个字段的语句时,ex 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();
?>
答案 0 :(得分: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());
答案 1 :(得分: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());