Because of the keys you put in, it seems that some code is missing there, you should review the question.
If what you put is what you have it, the problem is in the SELECT p.id_articulo...
that should be SELECT p.id_producto...
I am very fussy about the names used and I think it is a good practice to use a series of patterns, which in addition to streamlining the analysis and programming, facilitate the exchange of code and group programming.
Some basic rules would be:
Table names always in plural. For example: categorias
The Primary Key
field always exists, always is a autoincremental and always is called id
The Foreing Keys
always are called id
+ _
+ nombre_de_tabla_en_singular
: For example: id_categoria
Never repeat the name of the table in a field, for example: nombre_articulo
in table articulos
. It is redundant information,
because just putting nombre
already suggests that it is the name of the
article (the table or the alias will indicate it). If i was
justified using the name of the table to "facilitate" understanding
it should also be used in the other fields: color_articulo
,
familia_articulo
, precio_articulo
.... and that is not usually done.
It also allows us to use generic methods or properties that will work most of the time, for example: $registro->nombre
We know that it will work for most of the queries, be it the table articulos
, categorias
or clientes
, with which we obtain a more reusable code.
Use tables / fields names semantically consistent with their
functionality, using complete and separated words by low script in case
of multiple words, for example, the table permRespAlm
would be permisos_responsable_almacen
with which a more readable code is generated and therefore easier to maintain, even if it is longer.
This way your tables would be:
TABLA: articulos
id nombre id_categoria
===================================================
1 Prueba 2
TABLA: categorias
id nombre
==================================
1 Prueba1
2 Prueba2
And the query called correctly (as indicated by @ ivan.depi) iterating over the result:
<?php
$id_del_articulo = $_REQUEST['id'];
$sql = ' SELECT a.id, a.id_categoria, a.nombre, c.nombre as categoria
FROM articulos AS a
INNER JOIN categorias AS c ON c.id = a.id_categoria
WHERE a.id = '.$id_del_articulo;
$query = $con->query($sql);
while ($row = $query->fetch_object()) {
echo " Artículo: $row->nombre Categoría: $row->categoria <br>".PHP_EOL ;
}
?>