Get data from one table from the foreign key of another
772
I am trying to get the name of a relative from the "family" table that has the primary key of the "user" table as a foreign key. Do you know in which part I will have the error? It doesn't show me anything inside the while, not even the output of the var_dump
I didn't try your code but maybe it will be solved like this: create a variable after the fetch_array something like this: $id = $ids['id']; Then put the name of that new variable in the where of the query. I guess you are not defining the id in any variable
What happens is that you are misconstructing the query to get the name of the relative. I'll explain.
You are putting the variable $idsinside the query, to filter by id_usuario. This is no doubt because you assume that it $idsis an integer (the one idin usuario).
However, it $idsis an array, since you are assigning the value returned by mysqli_fetch_array().
Therefore, to access the data idof the usuario, you will have to indicate the index of the corresponding element within that array. In this case, you can do it in two ways, since it mysqli_fetch_array()returns the indexed values both numerically and associatively.
Doing it by the numerical index, since only one field is requested in the query select, the index is 0. So the value of idwould be in $ids[0]. Being that way:
$datas = mysqli_query($conexion, "SELECT nombre FROM familiar WHERE id_usuario = ' . (int) $ids[0] . "'");
In addition, I have cast the data to intto avoid problems in the query.
But it may be clearer to use the associative index, which will be the name of the requested field itself: 'id'. So:
$datas = mysqli_query($conexion, "SELECT nombre FROM familiar WHERE id_usuario = ' . (int) $ids['id'] . "'");
With that it would work for you, but I tell you more things:
You should check that the first query, with which you get the id, executes correctly.
It would also be good if you check if you get none, one, or several results, and act accordingly in each case.
Likewise, you could check if the second query is executed correctly, or if it produces an error (this would have told you about the problem I am answering you about).
Also act accordingly if that second query returns none or some results.
In var_dump()it you have put double $$by mistake, it would be simple.
It would be convenient to escape the values obtained from the database when you show them inside the HTML, since they can contain some character that breaks your HTML code. You can use html_entity_decode()(do some research).
I didn't try your code but maybe it will be solved like this: create a variable after the fetch_array something like this: $id = $ids['id']; Then put the name of that new variable in the where of the query. I guess you are not defining the id in any variable
What happens is that you are misconstructing the query to get the name of the relative. I'll explain.
You are putting the variable
$ids
inside the query, to filter byid_usuario
. This is no doubt because you assume that it$ids
is an integer (the oneid
inusuario
).However, it
$ids
is an array, since you are assigning the value returned bymysqli_fetch_array()
.Therefore, to access the data
id
of theusuario
, you will have to indicate the index of the corresponding element within that array. In this case, you can do it in two ways, since itmysqli_fetch_array()
returns the indexed values both numerically and associatively.Doing it by the numerical index, since only one field is requested in the query
select
, the index is0
. So the value ofid
would be in$ids[0]
. Being that way:In addition, I have cast the data to
int
to avoid problems in the query.But it may be clearer to use the associative index, which will be the name of the requested field itself:
'id'
. So:With that it would work for you, but I tell you more things:
id
, executes correctly.var_dump()
it you have put double$$
by mistake, it would be simple.html_entity_decode()
(do some research).