我再次遇到 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 和 m
。
我想创建一个 SELECT COUNT(*) FROM
usersWHERE 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();
?>
最佳答案
尝试将您的 $_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());
关于php - MYSQL 表对字段挑剔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17357302/