简体   繁体   English

PHP和MySql-从两个表中获取行

[英]PHP and MySql - fetching rows from two tables

I made product site where I fetch datas about produtcs from database, and I fetch datas about user who posted that product. 我在产品站点上从数据库中获取有关产品的数据,并获取了有关发布该产品的用户的数据。

In one table are datas for product and in another table are datas for user, in product table is row with id of user who posted that product. 在一个表中是产品数据,在另一个表中是用户数据,在产品表中是具有发布该产品的用户ID的行。

Now, I want to fetch both datas on same page, I don't really know how to do it. 现在,我想在同一页面上获取这两个数据,我真的不知道该怎么做。

This I made so far: 我到目前为止所做的:

    <?php
        include 'init.php';

        $id = sanitize($_GET['id']);
        $seller_id = sanitize($_GET['sid']);
        $eur = 7.544967;

        mysql_query(" UPDATE products SET view_count = view_count + 1 WHERE id = '$id' ");  

    $query = mysql_query("SELECT * FROM products, users INNER JOIN product.seller_id = users.id WHERE product.id=".$id);        



while($result = mysql_fetch_assoc($query)){
                    $product_name = $result['product_name'];
                    $img_path = $result['img_path']; 
                    $img_name = $result['img_name'];
                    $condition = $result['condition'];
                    $quantity = $result['quantity'];
                    $country = $result['country'];
                    $price = $result['price'];
                    $pay_method = $result['pay_method'];
                    $shipping = $result['shipping'];
                    $return = $result['return'];
                    $description = $result['description'];                  

                    echo '<div id="sub_container">
                            <div id="image_container">                  
                                <div class="thumb-image">
                                    <img src="'.$img_path.'/'.$img_name.'" data-imagezoom="true" width="500px" height="500px"> 
                            </div>
                            <ul id="img_ul">
                                <li><img src="#" width="80px" height="80px"/></li>
                                <li><img src="#" width="80px" height="80px"/></li>
                                <li><img src="#" width="80px" height="80px"/></li>
                            </ul>                   
                        </div>
                        <div id="product_container">
                            <p><strong>'.$product_name.'</strong></p>
                            <hr>
                            Stanje: '.$condition.' <br><div class="br"></div>
                            Količina: '.$quantity.' <br><div class="br"></div>
                            Zemlja porijekla: '.$country.'<br><div class="br"></div><br>
                            Cijena: '.$price.'kn (~'.round($price/$eur).'€)<div class="br"></div>
                            Način plačanja: '.$pay_method.'<div class="br"></div>
                            Dostava: '.$shipping.'<div class="br"></div>
                            Povrat proizvoda: U roku od '.$return.' dana.<br><div class="br"></div>
                            <div class="br"></div><br><div class="br"></div>                                    
                        </div>
                        <ul id="aside_container">
                            <li>
                                <div id="aside">                    
                                    <img src="'.$seller_img_path.'/'.$seller_img_name.'" width="50px" height="50px"/>
                                    <a href="#">'.$seller_username.'</a>                    
                                    <br>
                                    '.$seller_points.'
                                    <hr>
                                    Broj pregleda: '.$result['view_count'].'<br>
                                    <a href="#">Dodaj na karticu</a><br>
                                    <a href="#">Dodaj u listu želja</a><br>
                                </div>
                            </li>
                            <li>
                                <div id="aside_buy">
                                    Boja: <select>
                                            <option value="red">Crvena</option>
                                            <option value="blue">Plava</option>
                                        </select><br><div class="br"></div>
                                    Veličina: <select>
                                        <option value="X">X</option>
                                        <option value="XL">XL</option>
                                    </select><br><div class="br"></div>
                                    Količina: <input type="number" name="quantity" id="quantity" value="1"/><br><br><br>
                                    <a id="buy_button" href="#">Kupi proizvod</a>
                                </div>
                            </li>
                        </ul>           
                    </div>
                    <div id="description_container">
                        <p><strong>Opis proizvoda</strong></p>
                        <hr>                        
                        <div id="description">'.$description.'</div>
                    </div>';
    ?>

$user_query is query for fetching datas from user table and vars in my code with seller word are fetched from that table, I tried with while loop but then my site is slow. $user_query是用于从用户表中获取数据的查询,而在我的代码中带有seller单词的vars是从该表中获取的,我尝试了while循环,但随后我的网站运行缓慢。

I just had to join my tables then run $query . 我只需要加入我的表,然后运行$query

Like this: 像这样:

$query = mysql_query("SELECT * FROM products, users WHERE products.seller_id=users.id AND products.id='$id'");

Thanks everybody for help. 谢谢大家的帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM