第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在php PDO中加入兩個不同的表

如何在php PDO中加入兩個不同的表

PHP
紅顏莎娜 2021-10-08 21:31:45
我有兩個包含此內(nèi)容的表:Table users:id                       1username                 demoUserpwd                      123uid                      ghuyd3t2fgaggascxucxtu767fjc1g1eTable all_product:id                       1p_name                   demoNmaeprice                    demoproduct_id               ghuyd3t2fgaggascxucxtu767fjc1g1e我想加入他們并獲取數(shù)據(jù),這是我正在使用的代碼:$uid = $_GET['pid'];$query = "SELECT users.*, all_product.* FROM users tableUsers JOIN all_product tableProduct ON tableUsers.uid = tableProduct.product_id WHERE tableProduct.product_id = tableUsers.$uid";$statement = $con->prepare($query);$statement->execute();$result = $statement->fetchAll();foreach($result as $row){  echo $row['id'];  echo $row['username'];  echo $row['p_name'];}但我收到了這個錯誤:致命錯誤:未捕獲的異常 'PDOException' 帶有消息 'SQLSTATE[42S02]:未找到基表或視圖:1051 未知表 'users' 在 /Applications/AMPPS/www/AppenceMedia/fetch_user.php:22 堆棧跟蹤:#0 /Applications/AMPPS/www/AppenceMedia/fetch_user.php(22): PDOStatement->execute() #1 {main} 在第 22 行的 /Applications/AMPPS/www/AppenceMedia/fetch_user.php 中拋出
查看完整描述

3 回答

?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

您正在使用 table 的別名,但在 select 中您使用的是 table 的名稱..這會給您帶來錯誤。


同樣在 where 條件綁定參數(shù)


試試這個代碼:


    $uid = $_GET['pid'];


    $query = "SELECT tableUsers.*, tableProduct.* FROM users tableUsers JOIN all_product tableProduct ON tableUsers.uid = tableProduct.product_id WHERE tableProduct.product_id = :product_id";


    $statement = $con->prepare($query);

    $statement->bindParam(':product_id', $uid, PDO::PARAM_STR);

    $statement->execute();

    $result = $statement->fetchAll();


    foreach($result as $row){

      echo $row['id'];

      echo $row['username'];

      echo $row['p_name'];

    }


查看完整回答
反對 回復 2021-10-08
?
浮云間

TA貢獻1829條經(jīng)驗 獲得超4個贊

我認為您的查詢有點錯誤。你應該這樣做:

table_name as table_alias

所以在你的情況下:

$query = "SELECT users.*, tableProduct.* FROM tableUsers  as users JOIN all_product as tableProduct ON tableUsers.uid = tableProduct.product_id WHERE tableProduct.product_id = tableUsers.$uid";

br


查看完整回答
反對 回復 2021-10-08
?
暮色呼如

TA貢獻1853條經(jīng)驗 獲得超9個贊

確保你真的有一個名為 users 的表,如果你使用表別名,請嘗試在任何地方使用這些


$query = "SELECT tableUsers.*, tableProduct.* 

FROM users tableUsers 

JOIN all_product tableProduct ON tableUsers.uid = tableProduct.product_id 

WHERE tableProduct.product_id = tableUsers.$uid";

無論如何你不應該在 sql 中使用 php var .. 你有 sqljection 的風險.. 嘗試看看準備好的語句和綁定參數(shù)


查看完整回答
反對 回復 2021-10-08
  • 3 回答
  • 0 關(guān)注
  • 184 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號