list publications of friends

Asked

Viewed 36 times

-2

i am making a query in the database to list my publications and the people I follow on my Timeline, the more the query just this bringing my publications.

<?php

$tipo_news = "SELECT * FROM $table_post WHERE usuario=$cod_login order by codigo desc";
$tipo_news_resultado = mysqli_query($mysqli, $tipo_news);
$tipo_news_quantidade = mysqli_num_rows($tipo_news_resultado);
            
$consulta_contatos = "SELECT * FROM $table_contatos WHERE usuario=$cod_login || amigo=$cod_login";
$resultado_contatos = mysqli_query($mysqli, $consulta_contatos);
$quantidade_contatos = mysqli_num_rows($resultado_contatos);

for($i=0;$i < $tipo_news_quantidade;$i++)
{
$vetor_news = mysqli_fetch_array($tipo_news_resultado);




?>
<?php print($vetor_news['nickname']); ?>
<?php print($vetor_news['post']); ?>

 <?php
}


?>  

1 answer

0

if I got it right it should be SQL more or less like this :

search Ids of my contacts:

$contatos_ids_query = "SELECT id FROM $table_contatos WHERE usuario=$cod_login"; $lista_ids = mysqli_query($mysqli, $contatos_ids_query);

Transform array with string Ids

$string_ids = implode(',', (array)$lista_ids);

search for posts from my contacts:

$contatos_news_query = "SELECT * FROM $table_post WHERE usuario in($string_ids) order by codigo desc";

$contatos_news = mysqli_query($mysqli, $contatos_news_query);

var_dump($contatos_news)

  • hi, thanks for the help I tried to do this way and gave error, I will try to explain better the question, I am making a query in the database to list my publications and the people I follow on my Timeline, plus the consultation just this bringing my publications.

  • It shows the error?

  • Fatal error: Uncaught Typeerror: implode(): Argument #2 ($array) must be of type ? array, string Given in C: xampp htdocs social usuario.php:650 Stack trace: #0 C: xampp htdocs social usuario.php(650): implode(',', 'SELECT F...') #1 {main} thrown in C: xampp htdocs social usuario.php on line 650

  • Turn $contacts_ids into array, $string_ids = implode(',', (array) $contacts_ids);

  • check if contacts_ids is returning an object with contact id list if positive turns into array $string_ids = implode(',', (array) $contacts_ids)

  • Calm down, I noticed that you ran over a few steps, you threw SQL straight into $contactos_ids, this is just a model to follow the logic

  • need by SQL of $contacts_ids in mysqli_query() mysqli_query(contacts_ids)

  • sorry I don’t know how to do it that way.

  • I will improve the above answer

  • I put more details in the answer try now.

  • gave error no for. Parse error: syntax error, Unexpected token "for" in C: xampp htdocs social usuario.php on line 663

Show 6 more comments

Browser other questions tagged

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