gpt4 book ai didi

php - 如何从多个 SQL 表中获取值并在 html/php 表中显示?

转载 作者:行者123 更新时间:2023-11-29 00:02:24 25 4
gpt4 key购买 nike

我有一个名为 result 的 SQL 数据库,其中包含表 ressub。例如,res 表的列和内容是:

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 表的列和内容是:

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

我想从表 res 和表 sub 中获取一些值并显示在 php/html 表中。

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']}";

关于php - 如何从多个 SQL 表中获取值并在 html/php 表中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com