我试图做一个if else语句,它将回应'完整'

时间:2014-08-16 14:02:44

标签: php mysql if-statement

我认为问题在于我的if语句。当数据库中存在testname和student name的特定值时,我希望代码回显完成,否则显示testname未完成。使用我现在的代码,无论我输入什么testname和名称,最后一个echo语句都会出现。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Quiz results </title>


<?php 
// Created Aug 14th to workout Lesson list to disappear when test is finished

DEFINE ('DB_SERVER', 'localhost');
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PSWD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'ccgate_amendb5');

$dbcon = mysql_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
$db = 'ccgate_amendb5';

mysql_select_db("$db") or die(mysql_error());

echo " <h1> YOU ARE Connected now !! </h1>";

echo " Quiz Results active <br> <br> ********************<br>  ";



$query = mysql_query("select testname, name FROM kcpe15questions");



$row = mysql_fetch_assoc($query);


if ($row['kcse-hist-f2-04-01-02-urbanization'] && $row['name']=='Amen')
{ 
  echo 'Subject Completed'; 
}
else
{ 
  echo '<br> urbanization f2-04-01-02';
}


mysql_close($dbcon);

?>


</head>

<body>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

您没有在查询中选择列'kcse-hist-f2-04-01-02-urbanization',因此您的else语句一直在执行。

$query = mysql_query("select testname, name , kcse-hist-f2-04-01-02-urbanization FROM kcpe15questions");
$row= mysql_fetch_assoc($query);
if ($row['kcse-hist-f2-04-01-02-urbanization'] && $row['name']=='Amen')
{ 
 echo 'Subject Completed'; 
}

答案 1 :(得分:1)

我相信你的问题如下......

$row['kcse-hist-f2-04-01-02-urbanization']

我应该想象以上不是列名? $ row正在获取数据库的列名。

最好先尝试获取所有信息,然后将其存储在某处,然后创建if语句。