0
我有三个表用户,产品和用户产品。SQL连接返回的结果不是数据库中反映的结果
用户具有用户配置文件信息,产品具有主键,并且userproduct具有用于UserId和ProductId的两个标题,用于匹配哪个用户拥有哪个产品。
userproduct是建立在以下方式:
----------------------------------
| UserId (int) | ProductId (int) |
----------------------------------
| 1 | 1 |
----------------------------------
| 1 | 2 |
----------------------------------
我的PHP代码如下:
$userId = 1;
$stmt = $pdo->prepare('SELECT * FROM product join userproduct on product.ProductId = userproduct.UserId where UserId = ?');
$stmt->execute([$userId]);
$products = $stmt->fetchAll();
foreach ($products as $productsOwned)
{
echo $productsOwned['ProductName'];
}
我会以为这将回声出产品1,产品2。但是,它回显了Product1,Product1。
我已经查看过我的数据库几次,一切似乎都是按顺序的,但是因为我仍在学习SQL,所以我假设我在那里犯了一个错误。任何指导将不胜感激。
我认为你必须在'product.name'型我以为你的意思是这'product.productName',所以我改变了我的代码,但现在我得到以下错误:'注意:未定义的偏移量:0在第40行的C:\ xampp \ htdocs \ index.php中,第40行是:'echo $产品已自动['ProductName'];' –
尝试var_dump结果($ products)并让我知道你在那里得到了什么 – AfikDeri
我得到:'array(2){[0] => array(2){[“userId “] => int(1)[”productName“] => string(9)”Product1“} [1] => array(2){[”userId“] => int(1)[”productName“] = > string(13)“Product2”}}' –