如何从多个 SQL table 中获取值并在 html/php table 中显示?
How to get value from multiple SQL table and show that in html/php table?
我有一个名为 result
的 SQL 数据库,其中包含 table res
和 sub
。
例如,
res
table 列和内容是:
sno regno name sub1 sub2 sub3 sub4 sub5
1 1DU12CS100 student1 70 80 85 70 90
2 1DU12CS101 student2 75 70 90 80 70
3 1DU12EE015 student3 80 85 70 50 65
4 1DU14CS123 student4 88 85 85 90 70
5 1DU13ME050 student5 85 90 70 60 55
sub
table栏目和内容为:
Sno batname sub1 sub2 sub3 sub4 sub5
1 1DU12CS Maths English Hindi Urdu Social
2 1DU12ME Sanksrit Chinese Japanese French Dutch
3 1DU12EE Circuit Electrical Electronic Maths Hindi
4 1DU14CS Hindi Maths Urdu Science Maths
5 1DU13ME Computer Maths Electrical Mechanical GK
我想从 table res
和 table sub
中获取一些值并显示在 php/html table 中。
1DU12CS100 --
1DU ->college code
12 ->Student admission year
CS ->computer science
100->roll no of student
当有人在php表格中输入1DU12CS100时,结果应该是这样显示的...
Subjects Marks
Maths 70
English 80
Hindi 85
Urdu 70
Social 90
而当有人输入1DU13ME050时,则显示应该是
Subjects Marks
Computer 85
Maths 90
Electrical 70
Mechanical 60
GK 55
php表单代码为
<!DOCTYPE HTML>
<html>
<body>
<form action="result.php" method="post">
Enter your Reg No: <input type="text" name="regno"><br>
<input type="submit">
</form>
</body>
</html>
result.php代码是//这个php代码应该做哪些修改??
<?php
$servername = "localhost";
$username = "myresult";
$password = "abcdefg";
$dbname = "myresult";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$regno = mysqli_real_escape_string($conn, $_POST['regno']);
$sql = "SELECT * FROM myresult WHERE regno LIKE '$regno'"; // What changes should be here ??
$result = $conn->query($sql);
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
}
$resultset[] = $row;
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "{$row['sub1']}{$row['sub2']}"; // What changes should be made here??
// Print the data
while($row = mysql_fetch_row($result)) {
foreach($row as $_column) {
echo "{$_column}";
}
}
}
} else {
echo "Result Not Found";
}
$conn->close();
?>
$sql = "SELECT sub.batname, sub.sub1 as subsub1, sub.sub2 as subsub2, ...,
Res.sub1 as ressub1, ...
FROM res
JOIN sub ON sub.batname = substr(res.regno, 1, 7)
WHERE regno LIKE '$regno'";
(我没有列出所有的列。由于您将列命名为相同的名称,因此您必须指定别名。更改名称是个好主意 - subn 不是一个好名字。)
然后往下...
while ($row = $result->fetch_assoc()) {
echo "{$row['ressub1']}{$row['subsub1']}";
我只是在您的 sql 查询中的内部联接应该这样做。类似于:
SELECT * FROM res r INNER JOIN sub s ON r.id = s.id WHERE regno LIKE '1DU12CS100';
这看起来只是开头(这里有太多事情要做)但是对于初学者,您可以将开头的 SQL 查询修改为类似这样的内容,因为 batname 似乎作为开头存在所有 regno 变量
$sql = "SELECT regno as batname FROM myresult WHERE regno LIKE '{$regno}%'";
然后你可以把它变成一个变量,比如$batname = $row['batname']
然后您可以使用 $batname
作为在 table 之间链接数据的方法。但是,您将需要做更多的事情,因为存在一些确切的字符串,它们具有某种意义。因此您需要在 $batname
中的某些位置识别特定字符串以识别它们的含义。这将必须自行分解。最好创建一个新的 table 来标识主题名称以匹配由此创建的字符串。
换句话说,你的脚本真的只是一个开始。一旦您找到了识别字符串中每个元素的方法(再次查找 table 会让您的生活变得更轻松),您将能够开始编写 php 脚本。
这是您可以使用的查找示例 table
code name
--- ----
CS Computer Science
等(我很难说出这些代码是怎么回事,但我相信你知道它们都是什么意思)。您可能应该为每一件有意义的作品提取这些数据。
我有一个名为 result
的 SQL 数据库,其中包含 table res
和 sub
。
例如,
res
table 列和内容是:
sno regno name sub1 sub2 sub3 sub4 sub5
1 1DU12CS100 student1 70 80 85 70 90
2 1DU12CS101 student2 75 70 90 80 70
3 1DU12EE015 student3 80 85 70 50 65
4 1DU14CS123 student4 88 85 85 90 70
5 1DU13ME050 student5 85 90 70 60 55
sub
table栏目和内容为:
Sno batname sub1 sub2 sub3 sub4 sub5
1 1DU12CS Maths English Hindi Urdu Social
2 1DU12ME Sanksrit Chinese Japanese French Dutch
3 1DU12EE Circuit Electrical Electronic Maths Hindi
4 1DU14CS Hindi Maths Urdu Science Maths
5 1DU13ME Computer Maths Electrical Mechanical GK
我想从 table res
和 table sub
中获取一些值并显示在 php/html table 中。
1DU12CS100 --
1DU ->college code
12 ->Student admission year
CS ->computer science
100->roll no of student
当有人在php表格中输入1DU12CS100时,结果应该是这样显示的...
Subjects Marks
Maths 70
English 80
Hindi 85
Urdu 70
Social 90
而当有人输入1DU13ME050时,则显示应该是
Subjects Marks
Computer 85
Maths 90
Electrical 70
Mechanical 60
GK 55
php表单代码为
<!DOCTYPE HTML>
<html>
<body>
<form action="result.php" method="post">
Enter your Reg No: <input type="text" name="regno"><br>
<input type="submit">
</form>
</body>
</html>
result.php代码是//这个php代码应该做哪些修改??
<?php
$servername = "localhost";
$username = "myresult";
$password = "abcdefg";
$dbname = "myresult";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$regno = mysqli_real_escape_string($conn, $_POST['regno']);
$sql = "SELECT * FROM myresult WHERE regno LIKE '$regno'"; // What changes should be here ??
$result = $conn->query($sql);
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
}
$resultset[] = $row;
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "{$row['sub1']}{$row['sub2']}"; // What changes should be made here??
// Print the data
while($row = mysql_fetch_row($result)) {
foreach($row as $_column) {
echo "{$_column}";
}
}
}
} else {
echo "Result Not Found";
}
$conn->close();
?>
$sql = "SELECT sub.batname, sub.sub1 as subsub1, sub.sub2 as subsub2, ...,
Res.sub1 as ressub1, ...
FROM res
JOIN sub ON sub.batname = substr(res.regno, 1, 7)
WHERE regno LIKE '$regno'";
(我没有列出所有的列。由于您将列命名为相同的名称,因此您必须指定别名。更改名称是个好主意 - subn 不是一个好名字。)
然后往下...
while ($row = $result->fetch_assoc()) {
echo "{$row['ressub1']}{$row['subsub1']}";
我只是在您的 sql 查询中的内部联接应该这样做。类似于:
SELECT * FROM res r INNER JOIN sub s ON r.id = s.id WHERE regno LIKE '1DU12CS100';
这看起来只是开头(这里有太多事情要做)但是对于初学者,您可以将开头的 SQL 查询修改为类似这样的内容,因为 batname 似乎作为开头存在所有 regno 变量
$sql = "SELECT regno as batname FROM myresult WHERE regno LIKE '{$regno}%'";
然后你可以把它变成一个变量,比如$batname = $row['batname']
然后您可以使用 $batname
作为在 table 之间链接数据的方法。但是,您将需要做更多的事情,因为存在一些确切的字符串,它们具有某种意义。因此您需要在 $batname
中的某些位置识别特定字符串以识别它们的含义。这将必须自行分解。最好创建一个新的 table 来标识主题名称以匹配由此创建的字符串。
换句话说,你的脚本真的只是一个开始。一旦您找到了识别字符串中每个元素的方法(再次查找 table 会让您的生活变得更轻松),您将能够开始编写 php 脚本。
这是您可以使用的查找示例 table
code name
--- ----
CS Computer Science
等(我很难说出这些代码是怎么回事,但我相信你知道它们都是什么意思)。您可能应该为每一件有意义的作品提取这些数据。