为了使我的问题尽可能清晰(我之前因过于模糊而被抱怨),这是我在一个项目中使用的两个表的结构和数据。表 1 包含足球比赛列表,表 2 包含将在表 1 中记录的英国足球队。
我只是在从图书馆拿出的一本书中学习 SQL。我已按照有关连接表数据的说明进行操作。在这里,我只是尝试将数据回显到页面上,以确保我的查询在围绕该数据设置页面样式之前是正确的。
<?php
//Set DB Variables
$dbc = mysql_connect(host, username, password);
$db = mysql_select_db(database);
$results= mysql_query("SELECT 'tbl_games.game_ID', 'tbl_games.game_date', 'tbl_teams.team_name' FROM tbl_teams, tbl_games
WHERE 'tbl_games.team1_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team2_ID' = 'tbl_teams.team_ID' AND 'tbl_games.team1_score' IS NULL AND 'tbl_games.team2_score' IS NULL");
while ($row = mysql_fetch_array($results)) {
foreach ($row as $columnName => $results) {
echo 'Column name: '.$columnName.' Column data: '.$columnData.'<br/>';
}
}
?>
当我运行代码时,页面上没有打印任何错误,它只是不打印任何内容。但是有(或至少应该有)一些结果出现了。我在这里做错了什么?
--
-- Table structure for table `tbl_games`
--
DROP TABLE IF EXISTS `tbl_games`;
CREATE TABLE IF NOT EXISTS `tbl_games` (
`game_ID` int(6) NOT NULL AUTO_INCREMENT,
`team1_ID` int(4) NOT NULL,
`team2_ID` int(4) NOT NULL,
`team1_score` int(2) DEFAULT NULL,
`team2_score` int(2) DEFAULT NULL,
`game_date` date NOT NULL,
PRIMARY KEY (`game_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=121 ;
--
-- Dumping data for table `tbl_games`
--
INSERT INTO `tbl_games` (`game_ID`, `team1_ID`, `team2_ID`, `team1_score`, `team2_score`, `game_date`) VALUES
(1, 42, 34, NULL, NULL, '2013-08-23'),
(2, 159, 45, NULL, NULL, '2013-08-23'),
(3, 5, 122, NULL, NULL, '2013-08-23'),
(4, 67, 12, NULL, NULL, '2013-08-24'),
(5, 60, 155, NULL, NULL, '2013-08-24'),
(6, 78, 105, NULL, NULL, '2013-08-24'),
(7, 101, 156, NULL, NULL, '2013-08-24'),
(8, 134, 144, NULL, NULL, '2013-08-24'),
(9, 142, 47, NULL, NULL, '2013-08-24'),
(10, 13, 88, NULL, NULL, '2013-08-24'),
(11, 21, 120, NULL, NULL, '2013-08-24'),
(12, 19, 16, NULL, NULL, '2013-08-24'),
(13, 20, 123, NULL, NULL, '2013-08-24'),
(14, 26, 29, NULL, NULL, '2013-08-24'),
(15, 36, 51, NULL, NULL, '2013-08-24'),
(16, 77, 21, NULL, NULL, '2013-08-24'),
(17, 81, 84, NULL, NULL, '2013-08-24'),
(18, 85, 18, NULL, NULL, '2013-08-24'),
(19, 132, 96, NULL, NULL, '2013-08-24'),
(20, 162, 50, NULL, NULL, '2013-08-24'),
(21, 22, 131, NULL, NULL, '2013-08-24'),
(22, 25, 152, NULL, NULL, '2013-08-24'),
(23, 86, 46, NULL, NULL, '2013-08-24'),
(24, 97, 27, NULL, NULL, '2013-08-24'),
(25, 107, 140, NULL, NULL, '2013-08-24'),
(26, 109, 115, NULL, NULL, '2013-08-24'),
(27, 127, 133, NULL, NULL, '2013-08-24'),
(28, 146, 69, NULL, NULL, '2013-08-24'),
(29, 150, 112, NULL, NULL, '2013-08-24'),
(30, 2, 38, NULL, NULL, '2013-08-24');
-- --------------------------------------------------------
--
-- Table structure for table `tbl_teams`
--
DROP TABLE IF EXISTS `tbl_teams`;
CREATE TABLE IF NOT EXISTS `tbl_teams` (
`team_ID` int(4) NOT NULL AUTO_INCREMENT,
`team_name` varchar(50) COLLATE latin1_general_ci NOT NULL,
PRIMARY KEY (`team_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=164 ;
--
-- Dumping data for table `tbl_teams`
--
INSERT INTO `tbl_teams` (`team_ID`, `team_name`) VALUES
(1, 'Aberdeen'),
(2, 'Accrington Stanley'),
(3, 'AFC Bournemouth'),
(4, 'AFC Wimbledon'),
(5, 'Airdrieonians'),
(6, 'Albion Rovers'),
(7, 'Aldershot Town'),
(8, 'Alfreton Town'),
(9, 'Alloa Athletic'),
(10, 'Annan Athletic'),
(11, 'Arbroath'),
(12, 'Arsenal'),
(13, 'Aston Villa'),
(14, 'Ayr United'),
(15, 'Barnet'),
(16, 'Barnsley'),
(17, 'Berwick Rangers'),
(18, 'Birmingham City'),
(19, 'Blackburn Rovers'),
(20, 'Blackpool'),
(21, 'Bolton Wanderers'),
(22, 'Bradford City'),
(23, 'Braintree Town'),
(24, 'Brechin City'),
(25, 'Brentford'),
(26, 'Brighton & Hove Albion'),
(27, 'Bristol City'),
(28, 'Bristol Rovers'),
(29, 'Burnley'),
(30, 'Burton Albion'),
(31, 'Bury'),
(32, 'Cambridge United'),
(33, 'Cardiff City'),
(34, 'Carlisle United'),
(35, 'Celtic'),
(36, 'Charlton Athletic'),
(37, 'Chelsea'),
(38, 'Cheltenham Town'),
(39, 'Chester'),
(40, 'Chesterfield'),
(41, 'Clyde'),
(42, 'Colchester United'),
(43, 'Coventry City'),
(44, 'Cowdenbeath'),
(45, 'Crawley Town'),
(46, 'Crewe Alexandra'),
(47, 'Crystal Palace'),
(48, 'Dagenham & Redbridge'),
(49, 'Dartford'),
(50, 'Derby County'),
(51, 'Doncaster Rovers'),
(52, 'Dumbarton'),
(53, 'Dundee'),
(54, 'Dundee United'),
(55, 'Dunfermline'),
(56, 'East Fife'),
(57, 'East Stirlingshire'),
(58, 'Elgin City'),
(59, 'England'),
(60, 'Everton'),
(61, 'Exeter City'),
(62, 'Falkirk'),
(63, 'FC Halifax Town'),
(64, 'Fleetwood Town'),
(65, 'Forest Green Rovers'),
(66, 'Forfar Athletic'),
(67, 'Fulham'),
(68, 'Gateshead'),
(69, 'Gillingham'),
(70, 'Greenock Morton'),
(71, 'Grimsby Town'),
(72, 'Hamilton Academical'),
(73, 'Hartlepool United'),
(74, 'Heart of Midlothian'),
(75, 'Hereford United'),
(76, 'Hibernian'),
(77, 'Huddersfield Town'),
(78, 'Hull City'),
(79, 'Hyde'),
(80, 'Inverness Caledonian Thistle'),
(81, 'Ipswich Town'),
(82, 'Kidderminster Harriers'),
(83, 'Kilmarnock'),
(84, 'Leeds United'),
(85, 'Leicester City'),
(86, 'Leyton Orient'),
(87, 'Lincoln City'),
(88, 'Liverpool'),
(89, 'Livingston'),
(90, 'Luton Town'),
(91, 'Macclesfield Town'),
(92, 'Manchester City'),
(93, 'Manchester United'),
(94, 'Mansfield Town'),
(95, 'Middlesbrough'),
(96, 'Millwall'),
(97, 'Milton Keynes Dons'),
(98, 'Montrose'),
(99, 'Morecambe'),
(100, 'Motherwell'),
(101, 'Newcastle United'),
(102, 'Newport County'),
(103, 'Northampton Town'),
(104, 'Northern Ireland'),
(105, 'Norwich City'),
(106, 'Nottingham Forest'),
(107, 'Notts County'),
(108, 'Nuneaton Town'),
(109, 'Oldham Athletic'),
(110, 'Oxford United'),
(111, 'Partick Thistle'),
(112, 'Peterborough United'),
(113, 'Peterhead'),
(114, 'Plymouth Argyle'),
(115, 'Port Vale'),
(116, 'Portsmouth'),
(117, 'Preston North End'),
(118, 'Queen of the South'),
(119, 'Queen''s Park'),
(120, 'Queens Park Rangers'),
(121, 'Raith Rovers'),
(122, 'Rangers'),
(123, 'Reading'),
(124, 'Republic of Ireland'),
(125, 'Rochdale'),
(126, 'Ross County'),
(127, 'Rotherham United'),
(128, 'Salisbury City'),
(129, 'Scotland'),
(130, 'Scunthorpe United'),
(131, 'Sheffield United'),
(132, 'Sheffield Wednesday'),
(133, 'Shrewsbury Town'),
(134, 'Southampton'),
(135, 'Southend United'),
(136, 'Southport'),
(137, 'St Johnstone'),
(138, 'St Mirren'),
(139, 'Stenhousemuir'),
(140, 'Stevenage'),
(141, 'Stirling Albion'),
(142, 'Stoke City'),
(143, 'Stranraer'),
(144, 'Sunderland'),
(145, 'Swansea City'),
(146, 'Swindon Town'),
(147, 'Tamworth'),
(148, 'Torquay United'),
(149, 'Tottenham Hotspur'),
(150, 'Tranmere Rovers'),
(151, 'Wales'),
(152, 'Walsall'),
(153, 'Watford'),
(154, 'Welling United'),
(155, 'West Bromwich Albion'),
(156, 'West Ham United'),
(157, 'Wigan Athletic'),
(158, 'Woking'),
(159, 'Wolverhampton Wanderers'),
(160, 'Wrexham'),
(161, 'Wycombe Wanderers'),
(162, 'Yeovil Town'),
(163, 'York City');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;