gpt4 book ai didi

PHP MySQL 表链接

转载 作者:行者123 更新时间:2023-11-30 23:44:33 25 4
gpt4 key购买 nike

当我从我的表打印我的结果时,学生的 ID 显示为它链接到的另一个表的 ID,而不是学生的姓名。

我从网站上得到的结果;

连接成功

id: 1 - Student: 1 - Time: 60 - Date: 2017-12-28 - Notes: First test

id:2 - 学生:2 - 时间:43 - 日期:2018-01-22 - 备注:第二次测试

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "timedrun";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
?>
<br>
<?php
$sql = "SELECT id, student, time, date, notes FROM times";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Student: " . $row["student"]. " - Time: " . $row["time"]. " - Date: " .$row["date"]. " - Notes: " .$row["notes"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

这是从名为“times”的数据库中导出的

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 04, 2018 at 01:40 PM
-- Server version: 10.1.26-MariaDB
-- PHP Version: 7.1.9

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `timedrun`
--

-- --------------------------------------------------------

--
-- Table structure for table `times`
--

CREATE TABLE `times` (
`ID` int(11) NOT NULL,
`student` int(11) DEFAULT NULL,
`time` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`notes` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `times`
--

INSERT INTO `times` (`ID`, `student`, `time`, `date`, `notes`) VALUES
(1, 1, 60, '2017-12-28', 'First test'),
(2, 2, 43, '2018-01-22', 'Second Test'),
(3, 3, 75, '2018-01-12', 'Thrid Test');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `times`
--
ALTER TABLE `times`
ADD PRIMARY KEY (`ID`),
ADD KEY `student` (`student`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `times`
--
ALTER TABLE `times`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `times`
--
ALTER TABLE `times`
ADD CONSTRAINT `times_ibfk_1` FOREIGN KEY (`student`) REFERENCES `students` (`ID`);
COMMIT;

/*!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 */;

这是从名为“students”的数据库中导出的

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 04, 2018 at 01:42 PM
-- Server version: 10.1.26-MariaDB
-- PHP Version: 7.1.9

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `timedrun`
--

-- --------------------------------------------------------

--
-- Table structure for table `students`
--

CREATE TABLE `students` (
`ID` int(11) NOT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`yearGroup` varchar(255) DEFAULT NULL,
`house` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `students`
--

INSERT INTO `students` (`ID`, `firstName`, `lastName`, `yearGroup`, `house`) VALUES
(1, 'Harold', 'Jones', 'E', 'K'),
(2, 'Joe', 'Blogs', 'D', 'K'),
(3, 'Cliff', 'Kloff', 'D', 'Tu');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;

/*!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 */;

最佳答案

你需要使用内连接:

$sql = "select A.firstname, A.lastname, A.time, A.date, A.notes from times B,students A WHERE B.student/ID = A.ID;";

首先在这里尝试学习基本命令 link

关于PHP MySQL 表链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095635/

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