dpalame Posted July 27, 2011 Share Posted July 27, 2011 Hi sorry I am such a noob but this should be easy for you guys. I am running a query and then I want to put the results into a variable as a string for another query. Here is the code and I will comment inside: $query = "select upc from wfItemSpecs WHERE (brand LIKE '%" . join("%' OR brand LIKE '%", $at1) . "%') ORDER BY brand,upc ASC "; $result=mysql_query($query); while ($row = mysql_fetch_array($result)) { echo $row['upc']."<br/>"; } // Everything works fine up until here and the results are returned on a separate line $at1 = array(); while ($row = mysql_fetch_array($result)) { $at1 = implode(",", $row); } $at1 = explode (" ", $at1); echo $at1." variable string"; // This returns "Array variable string" I know I am missing something simple. Any and all help would be appreciated. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/ Share on other sites More sharing options...
Maq Posted July 27, 2011 Share Posted July 27, 2011 Create the comma-separated string in the first mysql_fetch_array() call. The rest of the code is just redundant. Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1247951 Share on other sites More sharing options...
dpalame Posted July 27, 2011 Author Share Posted July 27, 2011 Thank you for the help! New problem the string returned is 0007839102188,00078391021880007839102211,00078391022110075792290741,0075792290741 when it should be 0007839102188,0007839102211,0075792290741 Here is the code: // run query and close connection $query = "select upc from wfItemSpecs WHERE (brand LIKE '%" . join("%' OR brand LIKE '%", $at1) . "%') ORDER BY brand,upc ASC "; $result=mysql_query($query); while ($row = mysql_fetch_array($result)) { $comma_separated = implode(",", $row); echo $comma_separated; } If I change the code to mysql_fetch_assoc it returns 000783910218800078391022110075792290741 without the commas. Any ideas? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1247979 Share on other sites More sharing options...
Maq Posted July 27, 2011 Share Posted July 27, 2011 You can implode the result set as well, but here is another example: while ($row = mysql_fetch_array($result)) { $comma_separated .= $row['upc'] . ","; } echo rtrim($comma_separated, ","); Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1248030 Share on other sites More sharing options...
dpalame Posted July 27, 2011 Author Share Posted July 27, 2011 Thanks for the code it works well, but returns the values as Array0007839102188,0007839102211,0075792290741. Why is the word Array attached to the beginning and is there a way to remove that. Here is the code as it is written on my page: // run query and close connection $query = "select upc from wfItemSpecs WHERE (brand LIKE '%" . join("%' OR brand LIKE '%", $at1) . "%') ORDER BY brand,upc ASC "; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $at1 .= $row['upc'] . ","; } $at1 = rtrim($at1, ","); echo $at1; Thanks for your help again. Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1248093 Share on other sites More sharing options...
dpalame Posted July 27, 2011 Author Share Posted July 27, 2011 I left trimmed "Array", but is there a cleaner way? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1248105 Share on other sites More sharing options...
Maq Posted July 27, 2011 Share Posted July 27, 2011 Did you remove $at1 = array(); and replace it with $at1 = ""; ? Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1248113 Share on other sites More sharing options...
dpalame Posted July 27, 2011 Author Share Posted July 27, 2011 Ahhh had to clear out the variable! Thanks great help, my problem is solved. Quote Link to comment https://forums.phpfreaks.com/topic/242961-setting-query-result-into-string-variable-to-be-used-in-another-query/#findComment-1248117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.