我想显示所有已过期的记录。但是这只显示了过期的最后一条记录。
代码包含页面。 test.php和test1.php
test.php代码是:
<?php
include("database/db.php");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
@$result = mysqli_query($link,"select * from advertiser,company_information,advertisement,ads_date
WHERE Expiry_Date < CURDATE()");
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result))
{
$dateid = $row['Ads_date_id'];
$name=$row['Name'];
$cnic= $row['CNIC'];
$compname = $row['Company_Name'];
$adstitle = $row['Ads_Title'];
$startdate = $row['Starting_Date'];
$expiredate = $row['Expiry_Date'];
}
}
else
{
echo '<script type="text/javascript">';
echo 'alert("No Record Found");';
echo 'window.location.href = "SingleCustomer.php";';
echo '</script>';
}
mysqli_close($link);
?>
<?php
include("test1.php");
?>
test1.php代码是:
<html>
<head>
<title> Advertisement Information</title>
</head>
<body>
<table border="1">
<tr>
<td><?php echo $name; ?></td>
<td> <?php echo $cnic; ?></td>
<td> <?php echo $compname; ?></td>
<td> <?php echo $adstitle; ?></td>
<td> <?php echo $startdate; ?></td>
<td> <?php echo $expiredate; ?></td>
<td> <?php echo $dateid; ?> </td>
</tr>
</table>
</body>
</html>
我在MYSQl中有多个表。
1.advertiser
2.company_information
3.advertisement
4.ads_date
广告客户:
CREATE TABLE `advertiser` (
`Adv_id` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(20) NOT NULL,
`F_Name` char(20) NOT NULL,
`Address` varchar(40) NOT NULL,
`CNIC` int(13) NOT NULL,
`Contact` int(11) NOT NULL,
`Monthly_fee` varchar(10) NOT NULL,
`Region` varchar(10) NOT NULL,
`Reg_date` varchar(10) NOT NULL,
PRIMARY KEY (`Adv_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
2.Company_information:
CREATE TABLE `company_information` (
`Company_id` int(11) NOT NULL AUTO_INCREMENT,
`Company_Name` varchar(20) NOT NULL,
`Company_Contact` int(11) NOT NULL,
`Company_Address` varchar(30) NOT NULL,
PRIMARY KEY (`Company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
3.advertisement:
CREATE TABLE `advertisement` (
`Ads_id` int(11) NOT NULL AUTO_INCREMENT,
`Adv_id_id` int(11) NOT NULL,
`Company_id` int(11) NOT NULL,
`Ads_Title` varchar(20) NOT NULL,
`Ads_Description` varchar(40) NOT NULL,
`Ads_Image` varchar(50) NOT NULL,
PRIMARY KEY (`Ads_id`),
KEY `Adv_id` (`Adv_id_id`),
KEY `Company_id` (`Company_id`),
CONSTRAINT `Adv_id` FOREIGN KEY (`Adv_id_id`) REFERENCES `advertiser` (`Adv_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `advertisement_ibfk_1` FOREIGN KEY (`Company_id`) REFERENCES `company_information` (`Company_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
4.ads_date:
CREATE TABLE `ads_date` (
`Ads_date_id` int(11) NOT NULL AUTO_INCREMENT,
`Ads_id` int(11) NOT NULL,
`Starting_Date` varchar(30) NOT NULL,
`Expiry_Date` varchar(30) NOT NULL,
PRIMARY KEY (`Ads_date_id`),
KEY `Ads_id` (`Ads_id`),
CONSTRAINT `Ads_id` FOREIGN KEY (`Ads_id`) REFERENCES `advertisement` (`Ads_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
答案 0 :(得分:2)
问题是您在$name
循环中覆盖变量(while
等),然后只打印一次而不是在循环的每次迭代中打印它们。 E.g:
在test.php中:
$htmlTable = '';
while($row = mysqli_fetch_array($result)) {
$htmlTable .= '<tr><td>' . row['Ads_date_id'] .
. '</td><td>' . $row['Name']
. '</td><td>' . $row['CNIC'];
. '</td><td>' . $row['Company_Name'];
. '</td><td>' . $row['Ads_Title'];
. '</td><td>' . $row['Starting_Date'];
. '</td><td>' . $row['Expiry_Date']
. '</td></tr>';
}
然后在test1.php中:
<html>
<head>
<title> Advertisement Information</title>
</head>
<body>
<table border="1">
<?php echo $htmlTable; ?>
</table>
</body>
</html>
编辑:
第二个问题是您使用的查询是错误的。通过仅列出from
子句中没有连接条件的所有表,您将执行笛卡尔连接(即,将每个表中的每个记录与来自其他表的所有其他记录进行匹配)。相反,您应该指定如何连接这些表,最好使用现代显式join
语法:
SELECT *
FROM advertisement a
JOIN company_information ci ON ci.company_id = a.company_id
JOIN advertiser ar ON ar.adv_id = a.adv_id
JOIN ads_date ad ON ad.ads_id = a.ads_id
WHERE expiry_date < CURDATE()