select two tables using Where

Asked

Viewed 60 times

0

I have a table of produtos (id,id_categoria,nome,descricao) and a table of imagens (id, FileName, id_produto).

I need to list product data and a product related table image by id.

$sql = $mysqli->query
("SELECT
   produtos.nome,
   imagens.FileName
FROM
   produtos
INNER JOIN
   imagens ON produtos.id_produto = imagens.id_produto
WHERE produtos.id_produto = '".$_GET['id']."'");
  • What error or difficulty you are encountering?

  • it’s not working

  • managed to understand my doubt?

  • You are trying to make a query with php and mysql and it is not working ? which error appears?

  • Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given

  • The first part of Join doesn’t seem right to me. It should be ON produtos.id = imagens.id_produto. And I advise to test the query in phpMyAdmin or equivalent with an id of your choice to better understand the problem

  • changed but did not work: Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given

  • Do echo($mysqli->error); to have a more concrete description of the error you have in the consultation

  • Maybe I didn’t know how to express myself, it’s the following: I’m listing products from a category by get. In this listing appears the products all straight, but is not appearing the image, which is in a third table. So I have 3 tables: - categories - products - pictures

  • So far so good: $sql = $mysqli->query("SELECT * FROM products WHERE id_categoria = '". $_GET['id']."'");

Show 5 more comments

1 answer

0

The error presented:

Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given

Indicates some problem with the query. It may be a field with wrong name, or the id is being incorrectly passed. To be sure, try putting this code after your $mysqli->query command, to see what might be wrong

printf("Error: %s\n", mysqli_error($con));
  • hasn’t changed much: Error: Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given

  • Is your $_GET['id'] carrying any data? It may be coming blank. Test by swapping the snippet for a fixed id, just to test. Ex.: (... ) WHERE products.id_product = "0001"

  • 1

    Maybe I didn’t know how to express myself, it’s the following: I’m listing products from a category by get. In this listing appears the products all straight, but is not appearing the image, which is in a third table. So I have 3 tables: - categories - products - pictures

  • So far so good: $sql = $mysqli->query("SELECT * FROM products WHERE id_categoria = '".$_GET['id']."'"); I just need to get the image in a third table

    1. You may not have a corresponding product ID in the image table. Is there an image related to this ID? 2) Checked if column names are correct, including considering Case-Sensitive? Filename may be different from filename 3) If you run this query directly in the database, it returns some result?
  • has yes: id, Filename and id_product

  • Cleber as I explained: So far it’s all right: $sql = $mysqli->query("SELECT * FROM products WHERE id_categoria = '".$_GET['id']."'"); I just need to get the image that is in a third table

  • Listing all Ok, just the image not listed

  • And the query you put at the beginning of the question? A with INNER JOIN. It returns something if you try to consult directly in the bank?

  • That syntax I researched and tried to apply, but not for sure.

  • You have a Phpmyadmin or some other interface that allows you to run the query directly, other than by PHP code? Apparently, everything is correct. I even created the tables here and I circled, no mistake :/

  • I’m at the host location, want to see the bank structure?

  • I have three tables & #Xa;categories (id, name) products (id, id_category, name, description) images (id, Filename, id_product) I am listing all products linked to category by id Get $sql = $mysqli->query("SELECT * FROM products WHERE id_categoria = '".$_GET['id'].""); Correct listing, but the products do not show the cover image.

  • What I needed was to get the picture table pictures, that’s all.

  • Maybe it wouldn’t even be the use of INNER JOIN

  • Anyway... it seems so simple, at the same time no.

  • Why not like this? "SELECT FileName FROM imagens WHERE id_produto = '".$_GET['id']."'"

  • Good night Cleber! So did not roll, because I need to get the id and name of the product that is in tbl products

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.