您可以计算返回的记录并使用条件语句来确定要显示的图像。例如使用您的代码:
/* Using a mysql query which is discouraged and will be depreceated in future */
// declare variables
$i = 0;
$lastRegisteredDate = date("F j, Y, g:i a", $database->getLastUserRegisteredDate());
// declare statement string
$stmt = "SELECT * FROM matningar WHERE `datum` > $lastRegisteredDate";
// execute query
$result=mysql_query($stmt);
// make sure query executed properly
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// manually count the number of results
while ($row = mysql_fetch_assoc($result)) {
$i++;
}
// display image based on conditions
if($i == 0) {
// display one image
}
else {
// display another image
}
正如旁注 mysql 函数将在即将发布的 PHP 版本中被弃用,所以我将开始考虑使用 PDO 或 mysqli 库进行 mysql 查询。
/* Using the PDO library */
// declare variables
$i = 0;
$lastRegisteredDate = date("F j, Y, g:i a", $database->getLastUserRegisteredDate());
// declare database handler
$DBH = new PDO( "mysql:host=$host;dbname=$dbname", $user, $pass );
// prepare query
$STH = $DBH->prepare( "SELECT * FROM matningar WHERE `datum` > ?" );
// execute query
$STH->execute( array( $lastRegisteredDate ) );
// set fetch mode
$STH->setFetchMode( PDO::FETCH_OBJ );
// manually count the number of results
while ( $row = $STH->fetch() ) {
$i++;
}
// display image based on conditions
if($i == 0) {
// display one image
}
else {
// display another image
}