Php 变量的值在if条件之前更改
index.phpPhp 变量的值在if条件之前更改,php,mysql,exit,Php,Mysql,Exit,index.php public function checkIfTeamExists($team) { $array = array(); try { $sth = $this -> db -> prepare("SELECT * FROM teams WHERE Name = :team"); $sth -> execute(array(':team' => $team)); foreach ($sth-
public function checkIfTeamExists($team) {
$array = array();
try {
$sth = $this -> db -> prepare("SELECT * FROM teams WHERE Name = :team");
$sth -> execute(array(':team' => $team));
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
$array[] = $row;
}
return $array;
} catch (Exception $e) {
header('Location: /error');
}
}
让我们将URL设置为localhost/team/Bulls
在这种情况下,$URL[2]是多头
$teamExists=$db->checkIfTeamExists$teamName;将$teamExists的值设置为数组
下一步,$teamExists=count$teamExists>0;将$teamExists的值设置为true在这种情况下,这是真的,因为数据库中确实存在多头
问题就出在这里:
如第5行所示,print$teamExists;出口有。它打印1,这是预期的结果。但是,如果我去掉退出部分,让脚本按原样运行,它只执行if-else条件的else部分。这意味着$teamExists的值已从1更改为1
我无法理解我做错了什么。值怎么会这样变化呢?如果真的很麻烦,我会将函数中的查询更改为选择count…,然后测试答案是否大于0并返回true或false。你的主要剧本是
$URL = explode("/", $_SERVER['REQUEST_URI']);
$teamName = $URL[2];
$teamExists = $db -> checkIfTeamExists($teamName);
$teamExists = (count($teamExists) > 0);
print $teamExists;exit();
if ($teamExists) {
$teamNameUpper = ucwords(strtolower($teamName));
}
else {var_dump($teamExists);}
我已经将打印更改为var_转储,但几乎所有不是假的内容都应该是真的。我很想知道你从var_转储中得到了什么。结果是.htaccess中的重新路由代码把事情搞砸了。
$URL = explode("/", $_SERVER['REQUEST_URI']);
$teamName = $URL[2];
$teamExists = $db -> checkIfTeamExists($teamName);
var_dump($teamExists);exit();
if ($teamExists) {
$teamNameUpper = ucwords(strtolower($teamName));
}
else {var_dump($teamExists);}