create variable from the result of a query and use that variable in another query

0

is the following, so doing a query in the database with php! The code I use is the following:

<?php

$rs = $pdo->query(" SELECT * FROM licitacoes WHERE ID_USER = '$IDLOGADO' ")->fetchAll();

if(!$rs){ print_r($pdo->errorInfo()); }foreach ($rs as $row){

    echo $row['ID_LICITI'] ;}

?>

The result of this query is: 2 | 0

I want to then transform this resulting into a variable so that I can use it in another query! Something like that

<?php

$rs = $pdo->query(" SELECT * FROM outratabela WHERE ID_LICITI = $resultado or  ID_LICITI = $resultado")->fetchAll();

if(!$rs){ print_r($pdo->errorInfo()); }foreach ($rs as $row){

    echo $row['ID_LICITI'] ;}

?>

How can I notice that I want to create a Where in the first $ result and if I have more than one result I want to add the OR according to the results.

I want to do this because the results of the first query are parameters to filter the results of the second query, but this "filter" will be given by the same amount of results from the previous query, which will be a maximum of 9 results.

Of course this was the way I thought (and it's not working very well, because I do not know how to do it, I'm lost here), but if I do it differently (a more facial)     

asked by anonymous 12.02.2015 / 14:04

1 answer

2

The simplest way would be to use an implode to transform the array of the first query into a string separated by commas and playing this in the% s of% sql class.

The problem is that variables passed directly in the sql statement are subject to sql injection attacks, as you are using the PDO ideally it would be useful to use prepared statements. The logic to resolve this problem can be seen here and here

$sql = 'SELECT * FROM outratabela WHERE ID_LICITI '; //segunda consulta.
if(count($rs) > 1){

   $sql .= 'IN('. implode($rs, ',') .')';

   $itens = $pdo->query($sql)->fetchAll();
}
    
12.02.2015 / 14:25