2

Hi i made a page on my website to find quest in my database.

The method i used for now, its working but i have to update it everytime.

$demande = "SELECT * FROM quest_quest WHERE id IN (584,585,586,587,589,599,601,617,608,615,616) LIMIT ".(($cPage-1)*$perPage).",$perPage"; 

instead i would like to query my database and use the id in my query

I have a table with TagID and IDquest, i want to make a query to hold all questid and use it on my query

Example:

$questsfind = mysql_query("SELECT * FROM `quest_statetag` WHERE idTag ='8'");

I want to have a variable like that (221, 587, 1155, 1255, 5585)

$demande = "SELECT * FROM quest_quest WHERE id IN ('".$questsfind."') LIMIT ".(($cPage-1)*$perPage).",$perPage"; 

nb: Sry for my english its not my mother tongue :S

1

2 Answers 2

2

Not sure if I understand the question, but I believe you are looking to hold a bunch of IDs in a variable (from a query) and then create another query from that?

$first = mysql_query("SELECT * FROM quest_quest WHERE id IN (1,2,3,4)");

if( ! $first )
    die('Something went wrong...');

$ids = array();

while( $row = mysql_fetch_assoc($first) ) {
    $ids[] = $row['id'];
}

$query_ids = implode(',', $ids);

$second = mysql_query("SELECT * FROM quest_statetag WHERE idTag IN ($query_ids)");

Hope that helps...

Sign up to request clarification or add additional context in comments.

5 Comments

How i can query my DB to have all the ID in the array ? $TagQueryAray0 = mysql_query("SELECT * FROM quest_statetag WHERE idTag ='8'"); This return me only one ID normaly the result should give me 25 id and more
Not sure what you mean. Are you trying to save the array of IDs to the database? The answer I posted would essentially return any result with one of those IDs...
$TagQueryAray0 = array(); $TagQueryAray0 = mysql_query("SELECT * FROM quest_statetag WHERE idTag ='8'"); $query_ids = implode(',', $TagQueryAray0); $demande = mysql_query("SELECT * FROM quest_quest WHERE id IN ($query_ids)");
I want to query a sql table to get ID of quests and use it in other query
Modified my answer. See above
1

first you have to convert your associative array to string

$questsfinds = implode(',', $questsfind);

Then you can try FIND_IN_SET method

SELECT * FROM quest_quest WHERE FIND_IN_SET(id,'$questsfinds');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.