gpt4 book ai didi

php - 如何从 PHP 中的连接表中获取列?

转载 作者:行者123 更新时间:2023-11-29 13:41:37 24 4
gpt4 key购买 nike

对于单个表,我通常会这样做:

$length = 42;
$result = mysql_query ('SELECT * FROM table WHERE length = "' . $length . '"', $dbconn);
$rowsfound = mysql_num_rows ($result);
if ($rowsfound == 1) {
$row = mysql_fetch_array ($result);
$tableid = $row ['table_id'];
$tablecloth = $row ['tablecloth'];
$height = $row ['height'];
...

但是如何从连接表中获取行,如下所示:

$chairid = 123;
$result = mysql_query ('SELECT * FROM table,chair WHERE chair.id = "' . $chairid . '" AND table.table_id = chair.table_id', $dbconn);
$rowsfound = mysql_num_rows ($result);
if ($rowsfound == 1) {
$row = mysql_fetch_array ($result);
$tableid = $row ['table.table_id'];
$tablecloth = $row ['table.tablecloth'];
$height = $row ['table.height'];
...

这不会返回 $table.tablecloth 中的任何值。我做错了什么?

最佳答案

使用 JOIN 来执行此操作

    SELECT * FROM table
INNER JOIN chair ON table.table_id = chair.table_id
WHERE chair.id = "' . $chairid . '"

关于php - 如何从 PHP 中的连接表中获取列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006661/

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