我正在尝试获取一些如下记录,但无法继续。我的代码在下面,请帮助纠正此问题。在这里,有一个变量$ evaid,我想从数据库中获取一条记录,但不能添加该变量以查询任何人,而无需添加“给我”的数据。
获取数据后,我将无法运行该代码If If语句的最后一部分,而它直接将最后一个消息作为“联系WebAdmin,因为找不到任何信息!”。我知道,我在这段代码中做错了一些事,但是需要一些帮助,因为我无法找到它。 :(
<?php
$evaid = "' + response[0].EvaluationId + '";
echo $evaid;
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '123456';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM eval Where evaid=$evaid';
mysql_select_db('ops');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Eva ID :{$row['evaid']} <br> ".
"Division: {$row['division']} <br> ".
"Sub Type: {$row['evastype']} <br> ".
"--------------------------------<br>";
}
if ($row['division'] === "666" && $row['evaid'] === $evaid) {
print '666';
}
elseif($row['division']==="11" || $row['division']==="22" || $row['division']==="33" && $row['evaid'] === $evaid) {
print '112222';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dd22dd" && $row['evaid'] === $evaid) {
print 'cd123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="po1122" && $row['evaid'] === $evaid) {
print 'c9o123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dgd11" && $row['evaid'] === $evaid) {
print 'cdgn666 ';
}
else {
print '<p style="border:2px solid MediumSeaGreen;"> Contact WebAdmin As Nothing Is Found !!! </p> ';
}
mysql_close($conn);
?>
答案 0 :(得分:0)
这里有几件事。
if的问题是您在while循环之外添加了if,因此ifs只会在查询的最后一行进行检查。
但是您的代码对于使用mysql也是不安全的。您可能需要研究PDO或mysqli。
这是while循环,应该是
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Eva ID :{$row['evaid']} <br> ".
"Division: {$row['division']} <br> ".
"Sub Type: {$row['evastype']} <br> ".
"--------------------------------<br>";
if ($row['division'] === "666" && $row['evaid'] === $evaid) {
print '666';
} elseif($row['division']==="11" || $row['division']==="22" || $row['division']==="33" && $row['evaid'] === $evaid) {
print '112222';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dd22dd" && $row['evaid'] === $evaid) {
print 'cd123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="po1122" && $row['evaid'] === $evaid) {
print 'c9o123';
}
elseif($row['division']==="cc22cc" && $row['evastype']==="dgd11" && $row['evaid'] === $evaid) {
print 'cdgn666 ';
}
else {
print '<p style="border:2px solid MediumSeaGreen;"> Contact WebAdmin As Nothing Is Found !!! </p> ';
}
}