Mentales
Mentales

Reputation: 135

WHERE id=(array)

The code below only outputs single line(there are 2 in database that should be outputed).

I think that problem is in id=$data[id] since data1 is array instead of single value.I hoped that while will fix that but it doesnt look too good...

    $results1 = mysql_query("SELECT * FROM keywords WHERE keyword='$search' ORDER BY (relevant-irrelevant) DESC");
    $data1=mysql_fetch_array($results1);
    $results2=mysql_query("SELECT * FROM searchengine WHERE id='$data1[id]'");
    while($data2=mysql_fetch_array($results2))

Upvotes: 0

Views: 987

Answers (6)

George Cummins
George Cummins

Reputation: 28906

First, isolate your ids, looping to get all of the results:

$ids = array();

while ( $data1 = mysql_fetch_array($results1) ) {
    $ids[] = $data1['id'];
}

Then, convert your $ids array into a string. An easy way to do this is via implode():

$results2=mysql_query(
    "SELECT * FROM searchengine WHERE id IN (" . implode(',', $ids) . ")"
    );

Upvotes: 5

jeroen
jeroen

Reputation: 91734

Maybe I´m missing something, but how can $data1['id'] be an array? it´s probably an integer and perhaps a string, but it's not an array. $data1['id'] is a single value; the value of field id in the keywords table

I think you just need to put curly quotes around the variable:

$results2=mysql_query("SELECT * FROM searchengine WHERE id='{$data1[id]}'");

or even better:

$results2=mysql_query("SELECT * FROM searchengine WHERE id=" . (int) $data1['id']);

If id is an integer that is.

And of course if the first query returns more than 1 result, you will have to loop through them as well.

Upvotes: 2

Katfish
Katfish

Reputation: 698

mads.ohm is correct about combining the two queries into a single query.

As for your problem with only getting one return value, your while loop is just overwriting the contents of $data2 each time through.

You could write something like this instead:

$i = 0;
$data2 = array();
while ($row = mysql_fetch_array($results2)) {

    $data2[$i] = $row;
    $i++;

}

In this case, $data2 is declared as an array, and each iteration of the while loop adds a row from the database to the array.

Upvotes: 0

RRStoyanov
RRStoyanov

Reputation: 1172

You can't pass array as condition. You should:

a. do a for(each) loop in the $data1 array and perform next actions

b. implode the array and search with IN. Example:

$commaSeparated = implode(",", $data1);

$results2=mysql_query('SELECT * FROM searchengine WHERE id IN ('.$commaSeparated.'));

Upvotes: 0

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53597

    $results1 = mysql_query("SELECT * FROM keywords WHERE keyword='$search' ORDER BY (relevant-irrelevant) DESC");
    $data1=mysql_fetch_array($results1);
//VERY DANGEROUS TO USE USER INPUT
$in = join(',',$data1['id']);
    $results2=mysql_query("SELECT * FROM searchengine WHERE id IN ({$in})");
    while($data2=mysql_fetch_array($results2))

Upvotes: 1

Mads Ohm Larsen
Mads Ohm Larsen

Reputation: 3715

Couldn't you just select the entire thing in one query?

SELECT *
FROM keywords k
     searchengine s
WHERE k.keyword='$search'
AND k.id = s.id

Upvotes: 1

Related Questions