无法显示数据库中的 table
Cannot display the table from database
无法显示数据库中的 table。每个程序都有一个 table,其中保存了一些分数。另外,还有一个程序table,其中保存了program id
和program name
。当我选择一个程序和一年时,出现以下错误:
No data found
Notice: Undefined variable: result in C:\XAMPP\htdocs\statistics\ac_directorPrograms.php on line 61
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\statistics\ac_directorPrograms.php on line 61
操作代码中的 result
出了点问题,我猜 prog_name
和 program
形成了表单。
表格:
<?php
include 'connect.php';
?>
<form name="myform" action="ac_directorPrograms.php" method="POST">
<b>Programs:<b/>
<select name="program">
<option value="Choose">Please select..</option>
<?php
$sql = mysql_query("SELECT prog_name FROM program");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['prog_name'] . "'>" . $row['prog_name'] ."</option>";
}
?>
</select><br/><br/>
<b>Year:<b/>
<select name="year">
<option value="Choose">Please select..</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option></select><br/><br/>
<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</div>
操作代码
<?php
include 'connect.php';
$year = $_POST['year'];
$program = $_POST['program'];
$years = array(
2005,
2006,
2007
);
$programs = array(
'bsc computer science',
'bsc psychology',
'ba finance',
'ba marketing',
'ba management'
);
if (in_array($program, $programs) && in_array($year, $years)) {
$sql = "SELECT a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program WHERE year=$year";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tbody>
<tr>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
</tbody>
<?php
while($program=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$program['a1']."</td>";
echo "<td>".$program['a2']."</td>";
echo "<td>".$pprogram['a3']."</td>";
echo "<td>".$pprogram['l1']."</td>";
echo "<td>".$program['l2']."</td>";
echo "<td>".$program['l3']."</td>";
echo "<td>".$program['l4']."</td>";
echo "<td>".$program['l5']."</td>";
echo "<td>".$program['l6']."</td>";
echo "<td>".$program['l7']."</td>";
echo "<td>".$program['lavg']."</td>";
echo "<td>".$program['r1']."</td>";
echo "<td>".$program['r2']."</td>";
echo "<td>".$program['u1']."</td>";
echo "<td>".$program['u2']."</td>";
echo "<td>".$program['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
这是变量作用域的问题。您已经在 if 条件内定义了 $result 并在 if 之外使用它。您需要在 if 条件之外定义 $result。
在操作代码中 include connect.php
之后,添加此...
$con=mysqli_connect("localhost","my_user","my_password","my_db");
并且不要忘记将 my_user
、my_password
、my_db
替换为您的用户名、密码和数据库名称。
也用这个 $result=mysqli_query($con,$sql);
替换此代码 $result = mysql_query($sql);
无法显示数据库中的 table。每个程序都有一个 table,其中保存了一些分数。另外,还有一个程序table,其中保存了program id
和program name
。当我选择一个程序和一年时,出现以下错误:
No data found
Notice: Undefined variable: result in C:\XAMPP\htdocs\statistics\ac_directorPrograms.php on line 61
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\statistics\ac_directorPrograms.php on line 61
操作代码中的 result
出了点问题,我猜 prog_name
和 program
形成了表单。
表格:
<?php
include 'connect.php';
?>
<form name="myform" action="ac_directorPrograms.php" method="POST">
<b>Programs:<b/>
<select name="program">
<option value="Choose">Please select..</option>
<?php
$sql = mysql_query("SELECT prog_name FROM program");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['prog_name'] . "'>" . $row['prog_name'] ."</option>";
}
?>
</select><br/><br/>
<b>Year:<b/>
<select name="year">
<option value="Choose">Please select..</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option></select><br/><br/>
<br/>
<input type="submit" value="submit" name="Submit">
<input type="reset" name="reset" value="Clear">
</form>
</div>
操作代码
<?php
include 'connect.php';
$year = $_POST['year'];
$program = $_POST['program'];
$years = array(
2005,
2006,
2007
);
$programs = array(
'bsc computer science',
'bsc psychology',
'ba finance',
'ba marketing',
'ba management'
);
if (in_array($program, $programs) && in_array($year, $years)) {
$sql = "SELECT a1,a2,a3,l1,l2,l3,l4,l5,l6,l7,lavg,r1,r2,u1,u2,u3 FROM $program WHERE year=$year";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="container">
<table id="table" width="900" border="1" cellspacing="1">
<tbody>
<tr>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>L3 </td>
<td>L4 </td>
<td>L5 </td>
<td>L6 </td>
<td>L7 </td>
<td>LAVG </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tr>
</tbody>
<?php
while($program=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$program['a1']."</td>";
echo "<td>".$program['a2']."</td>";
echo "<td>".$pprogram['a3']."</td>";
echo "<td>".$pprogram['l1']."</td>";
echo "<td>".$program['l2']."</td>";
echo "<td>".$program['l3']."</td>";
echo "<td>".$program['l4']."</td>";
echo "<td>".$program['l5']."</td>";
echo "<td>".$program['l6']."</td>";
echo "<td>".$program['l7']."</td>";
echo "<td>".$program['lavg']."</td>";
echo "<td>".$program['r1']."</td>";
echo "<td>".$program['r2']."</td>";
echo "<td>".$program['u1']."</td>";
echo "<td>".$program['u2']."</td>";
echo "<td>".$program['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
这是变量作用域的问题。您已经在 if 条件内定义了 $result 并在 if 之外使用它。您需要在 if 条件之外定义 $result。
在操作代码中 include connect.php
之后,添加此...
$con=mysqli_connect("localhost","my_user","my_password","my_db");
并且不要忘记将 my_user
、my_password
、my_db
替换为您的用户名、密码和数据库名称。
也用这个 $result=mysqli_query($con,$sql);
$result = mysql_query($sql);