P
P
Pavel Sergeev2015-06-03 11:02:22
PHP
Pavel Sergeev, 2015-06-03 11:02:22

How to select data from another table?

Hello! There is this code

<?php
                            $query = mysql_query("SELECT * FROM products", $connect);
                            if (!$query) {
                                echo "<div class='container'><p>Выборка данных из БД не произошла. Обратитесь к администратору.<p></div>";
                                exit(mysql_error());
                            }
                            if (mysql_num_rows($query) > 0) {
                                $result = mysql_fetch_array($query);
                                do {
                                    printf("<tr><td>%s</td><td>%s</td><td><a href='edit_product.php?product_id=%s'>%s</a></td><td>%s</td></tr>", $result['product_id'], $result['product_cat'], $result['product_id'], $result['product_name'], $result['product_price']);
                                } while ($result = mysql_fetch_array($query));
                            } else {
                                echo "<div class='container'><div class='alert alert-danger' role='alert'>Новости отсутствуют.</div></div>";
                                //exit(mysql_error());
                            }
                            ?>

When executed, the rows of the data table are displayed. Interested in such a question, how to make a query so that instead of 'product_cat (simply the category number is displayed), the names of these categories are displayed, which are stored in another database table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Postovoy, 2015-06-03
@no_body

$query = "SELECT `a`.*, `b`.* FROM `products` `a` LEFT JOIN `product_categories` `b` on `b`.`id`=`a`.`product_cat`";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question