简体   繁体   English

如何使用PDO / php从具有相同id的第二个表中提取数据?

[英]How do you pull a data from a second table that has the same id using PDO/php?

DESCRIPTION 描述

I use the script below to displays all rows in my floradstable. 我使用下面的脚本显示我的floradstable中的所有行。 I have a second table florinvtable that has identical columns. 我有第二个表florinvtable具有相同的列。 The Price column in floradstable = 0.00 whereas the Price column in florinvtable contains the real price. floradstable中的Price列为0.00,而florinvtable中的Price列包含实际价格。

$result = $pdo->prepare(
"SELECT floradstable.Brand, florinvtable.Price
FROM   floradstable INNER JOIN florinvtable
ON     floradstable.Barcode=florinvtable.Barcode
ORDER BY Brand");

$result->execute();

while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo $row['Brand']." ";
echo $row['florinvtable.Price']." ";
echo "<a href='submit-ads-florida.php?deleteid=".$row['Barcode']."'>DELETE</a><br>";
}

OBJECTIVE 目的

I would like to echo the price from florinvtable using the unique id(Barcode) both tables share. 我想用florinvtable回复价格,使用两个表共享的唯一ID(条形码)。

PROBLEM 问题

florinvtable.Price does not echo which leads me to believe it is echo(ing) from floradstable verses florinvtable. florinvtable.Price没有回应,这让我相信它是来自floradstable诗歌florinvtable的回声。

NOTE 注意

The current script works as is, without an error but does not pull Price FROM florinvtable 当前脚本按原样运行,没有错误,但没有拉出Price FROM florinvtable

With the select you defined: 使用您定义的选择:

$result = $pdo->prepare(
"SELECT floradstable.Brand, florinvtable.Price
FROM   floradstable INNER JOIN florinvtable
ON     floradstable.Barcode=florinvtable.Barcode
ORDER BY Brand");

the columns in the result set are just called Brand and Price . 结果集中的列只称为BrandPrice The table reference is to fetch the correct one, not part of the name. 表引用是获取正确的,而不是名称的一部分。

Try 尝试

echo $row['Price']." ";

Edit: I see you also use $row['Barcode'] , which would not be defined by that query! 编辑:我看到你也使用$row['Barcode'] ,该查询不会定义! You would need 你需要

$result = $pdo->prepare(
"SELECT floradstable.Brand, florinvtable.Price, floradstable.Barcode
FROM   floradstable INNER JOIN florinvtable
ON     floradstable.Barcode=florinvtable.Barcode
ORDER BY Brand");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 php-mysql 中使用 pdo 从一台服务器连接(拉取数据)到(插入 - 创建表)另一台服务器,可能是获取? - how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ? 执行OPENQUERY来从MySql Server提取数据,并使用PDO从php脚本内部将其引导到SQL表? - Executing OPENQUERY to pull data from MySql Server and inner joing it to SQL table from php script using PDO? 如何从不同的表中提取数据并使用php将其显示在同一页面的不同html表中? - how to pull data from different table and display it in different html table on a same page using php? 如何使用PDO提取数据? - How do you fetch data using PDO? 如何使用PDO从php文件插入数据? - How do I insert data from a php file using PDO? 如何通过单击JQuery按钮提取ID数据? - How do you pull id data with a JQuery button click? 如何从 table1 的 ID 中提取数据并通过 PHP 将其放入 table2 的 FOREIGN KEY - How to pull data from table1's ID and place it into table2's FOREIGN KEY via PHP 使用PDO PHP将数据从一个表移动到另一个表 - Move Data from one table to another table using PDO php 使用$ pdo从2个具有相同ID的表中获取数据 - Get data from 2 tables, of the same ID with $pdo PHP从一个表中获取ID,并从另一表中提取与这些ID相对应的数据 - PHP take id's from one table and pull data that corresponds with those id's from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM