使用PHP从SQL数据库中获取信息

时间:2017-03-20 13:22:36

标签: php mysql database

现在我已经创建了一个包含一些信息的SQL数据库。

我目前有三个包含以下列的表:

  

(1)表名=参议员; columns = Dnumber,名称   (2)表名=委员会;列= ID,委员会   (3)table name = memberCommittee; columns = ID,Dnumber

我的选择陈述:

  

选择DISTINCT s.name作为参议员FROM参议员作为s,委员会作为   c,memberCommittee为m WHERE s.Dnumber = m.Dnumber和c.ID = m.ID   和c.ID =“1001”;

这样做的目的是使用三个表获取信息并显示ID为“1001”的“名称”。我的select语句有效,因为它会向委员会中的人员显示ID为1001,但我不太了解获取信息的PHP部分。

这部分:

*

 $result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator 
     FROM senators as s, committees as c, memberCommittee as m 
    WHERE s.Dnumber =m.Dnumber and c.ID = m.ID and c.ID = "1001");  

    while ($row = mysqli_fetch_assoc($result)){
            unset($id, $Senator);           
            //$id = $row['Features'];   
            $Senator = $row['Senator'];
            echo '<option value="'.$Senator.'">'.$Senator.'</option>';      

    }

*

有人可以帮我吗?

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<script language="javascript" type="text/javascript">
			function dynamicdropdown(listindex) {
			  document.getElementById('senator').className = listindex;
			</script>
			
			<style>
			optgroup {
			  display: none;
			}

			select.Agriculture 
			optgroup.Agriculture
			{
			  display: block;
			}
			
			div#header{
			padding: 1px;
			color: yellow;
			padding-left: 9px;
			background-color: #000080;
			}
			
			.category_div{
			padding: 3px;
			}
			
			.sub_category_div{
			padding: 3px;
			}
			
			.microphone{
			padding: 3px;
			}
			
			.body{
			padding-right: 5px;
			}
			</style>
	</head>
	
<body>

	<div class="header" id="header">
	<h1>Indiana State Senate IT</h1>
	</div>

	<div class="room130">
	<h3>Room 130</h3>
	<form target="Room 130" action = "room130.php" method="POST">
		<div class="category_div" id="category_div">Committee:
			<select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
				<option value="">Select Committee</option>
				<option value="Agriculture">AGRICULTURE</option>
			</select>
		</div>
		
		<div class="sub_category_div" id="sub_category_div">
		Individual:
			<select name="senator" id="senator">
				<option value="">Select individual</option>
					<optgroup class="Agriculture">
						<?php
							$conn = mysqli_connect("db.soic.indiana.edu", "i308f16_team43", "my+sql=i308f16_team43", "i308f16_team43");

							if(!$conn){
									die("Connection Failed".myslqi_connect_error());
							}
									$result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator FROM senators as s, committees as c, memberCommittee as m WHERE s.Dnumber = m.Dnumber and c.ID = m.ID and c.ID = "1001");
									while ($row = mysqli_fetch_assoc($result)){
											unset($id, $Senator);
											//$id = $row['Features'];
											$Senator = $row['Senator'];
											echo '<option value="'.$Senator.'">'.$Senator.'</option>';
									}
							?>								 
					</optgroup>
				</option>
			</select>
		</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

你有一个错误的双引号序列(你应该将1001包裹在单引号

 $result = mysqli_query($conn, "SELECT DISTINCT s.name as Senator 
     FROM senators as s, committees as c, memberCommittee as m 
    WHERE s.Dnumber =m.Dnumber and c.ID = m.ID and c.ID = '1001'");     

并且您应该回显行结果

 echo '<option value="'.$row['Senator'].'">'. $row['Senator'] .'</option>';  

并确保你没有c.ID作为整数,在这种情况下,你应该使用单引号的1001

$result = mysqli_query($conn, "SELECT DISTINCT 
                            s.name as Senator 
                          FROM senators as s
                            , committees as c
                            , memberCommittee as m 
                          WHERE s.Dnumber =m.Dnumber 
                          and c.ID = m.ID 
                          and c.ID = 1001;");     

和(只是一个建议)你应该明确加入sintax)

$result = mysqli_query($conn, "SELECT DISTINCT 
                                s.name as Senator 
                              FROM senators as s
                              INNER  JOIN memberCommittee as m on s.Dnumber =m.Dnumber 
                              INNER JOIN  committees as c on  and c.ID = m.ID 
                              WHERE c.ID = 1001"); 

答案 1 :(得分:0)

mysqli_query()返回一个mysqli_result对象(失败时为false)。该对象有一个名为:: fetch_assoc()(或fetch_assoc(),过程样式)的方法,该方法不带参数并返回数组。 while用于迭代直到所有行返回(一次一个)到$ row变量。当$ row从fetch_assoc()获得false时,循环停止。 查看手册: http://php.net/manual/en/mysqli-result.fetch-assoc.php