简体   繁体   English

PHP的if语句总是返回false

[英]php if statement always returns false

I know this is a really basic question but I am completely at my wits end. 我知道这是一个非常基本的问题,但我完全不知所措。 I have been working on this for two days. 我已经为此工作了两天。 The if statement always returns false. if语句始终返回false。 I have tried === and == and copy to an $array and process outside the fetch $result loop. 我试过===和==并复制到$ array并在获取$ result循环之外进行处理。 I have compared to a direct "name". 我已经将其与直接的“名称”进行了比较。 I have outputed the ascii value of each letter of the returned string and compared that way to make sure I wasn't putting a space or something in somewhere. 我已经输出了返回字符串的每个字母的ascii值,并进行了比较,以确保没有在任何地方放置空格或其他东西。 I've tried mysql and mysqli ways. 我已经尝试了mysql和mysqli方法。 I've tried OO style and Procedural style but here's the rub, This code was copy and pasted from my own site where it's working just fine in three other programs in my site. 我尝试了OO风格和过程风格,但这是麻烦所在,此代码是从我自己的网站复制并粘贴的,在我的网站上的其他三个程序中也能正常工作。

<?php
SESSION_START();
if(!isset($_SESSION["uname"])){
require ('redirect.html');
die();
}// session is set close
$uname = $_SESSION["uname"];

$user_name = "xxxxxx";
$password = "xxxxxxx";
$database = "usersdata";
$server = "xxxxxxxxxx.ipagemysql.com";


$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM messages";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

//echo out values for visual comparison
echo $db_field['recipient']." ".$uname." ";

if($db_field['recipient'] === $uname){echo " match ";} else {echo " no match ";}

}// while loop db_field close
}// if db_found loop close

mysql_close($db_handle);

?>

As I stated this code works just fine in three other programs running on this very site. 正如我所说的,此代码在此站点上运行的其他三个程序中也可以正常工作。 All I did was copy paste and change the db and fields. 我所做的就是复制粘贴并更改数据库和字段。 I even tried retyping it all from scratch just to make sure. 我甚至尝试从头开始重新输入所有内容以确保。 Help me I'm melting.... 帮帮我,我正在融化...

Why would you return all the messages and then compare with every single message if the name matches. 如果名称匹配,为什么要返回所有消息,然后与每条消息进行比较。 You can simply add a where clause to your query 您只需在查询中添加where子句

SELECT * FROM messages WHERE recipient='uname value here'

Then you can check for the number of rows returned. 然后,您可以检查返回的行数。 If 0 rows are returned, there was no match. 如果返回0行,则没有匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM