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

Chris
  • 4,762
  • 3
  • 44
  • 79
Nicecube
  • 21
  • 1
  • 2

2 Answers2

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...

Chris
  • 4,762
  • 3
  • 44
  • 79
  • 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 – Nicecube Oct 17 '15 at 21:04
  • 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... – Chris Oct 17 '15 at 21:06
  • $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)"); – Nicecube Oct 17 '15 at 21:10
  • I want to query a sql table to get ID of quests and use it in other query – Nicecube Oct 17 '15 at 21:11
  • Modified my answer. See above – Chris Oct 17 '15 at 21:17
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');

Dharman
  • 30,962
  • 25
  • 85
  • 135
SANTHOSH K
  • 11
  • 1
  • 5