Gathering information from MySQL using two or more queries to retrieve on set of data

StackOverflow https://stackoverflow.com/questions/22392140

  •  14-06-2023
  •  | 
  •  

Domanda

I'm trying to get information from a tables in mysql to show a selection of popular products, I've come up with the following code so far with no joy:

$sql2 = "SELECT GROUP_CONCAT(session_id) AS session_group FROM counter WHERE unique_id = '$unique_id'";
$rs2 = mysql_query($sql2,$con);

while($row2 = mysql_fetch_array($rs2)) {
"<p>";
$session_group = str_replace(",",", ", $row2['session_group']);
"</p>";
}

echo $sql3 = "SELECT unique_id, product_name FROM counter WHERE session_id IN ( ".var_export($session_group, true)." )";
$rs3 = mysql_query($sql3,$con);

while($row3 = mysql_fetch_array($rs3)) {
echo "<p>";
echo $row3['product_name'] . "<br />";
echo $row3['unique_id'] . "<br />";
echo $row3['hit_count'] . "<br />";
echo "</p>";
}

The first query runs fine and displays the desired result but the second query doesn't display anything at all although I am certain there is information to be found there.

I've been looking for an answer all day and have already looked on the site for help but haven't quite figured out how to get this working. Any help would be appreciated, I will be looking back for responses in the morning.

Many thanks!

Costa

È stato utile?

Soluzione

The var_export($session_group, true) is the problem

you have :

SELECT unique_id, product_name FROM counter WHERE session_id IN ( 'upbnqxP57, upbnqxP57, upbnqxP57, upbnqxP57, upbnqxP57, upbnqxP57, upbnqxP57, upbnqxP57, aoCgRk635, aiEJuVH31, aiEJuVH31, aiEJuVH31, mHIwa0k20, mHIwa0k20' ) 

but you need:

SELECT unique_id, product_name FROM counter WHERE session_id IN ( 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'upbnqxP57', 'aoCgRk635', 'aiEJuVH31', 'aiEJuVH31', 'aiEJuVH31', 'mHIwa0k20', 'mHIwa0k20' ) 

'upbnqxP57, upbnqxP57' != 'upbnqxP57', 'upbnqxP57'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top