PHP MySQL表链接

时间:2018-01-04 12:44:53

标签: php html mysql sql

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

我从网站上得到的结果;

已成功连接

id:1 - 学生:1 - 时间:60 - 日期:2017-12-28 - 备注:首次测试

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

这是从数据库导出的名为“学生”

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

2 个答案:

答案 0 :(得分:0)

您需要使用内部联接:

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

首先尝试学习基本命令link

答案 1 :(得分:-1)

如果您需要更改PHP代码的学生姓名:

$sql = "SELECT id, student, time, date, notes FROM times";

$sql = "select b.firstname, b.lastname, a.time, a.date, a.notes from times a join students b on a.student = b.id;";

如果您打算使用关系数据库,例如MySQL,那么值得了解它们的工作原理以及如何使用SQL。这可能是非常有益的,并不是非常困难。如果你可以学习php,那么你可以学习SQL。这是one place you could start

相关问题