Seancon1 Posted September 12, 2012 Share Posted September 12, 2012 <?php $item1 = "one"; $item2 = "two"; $item3 = "three"; $k = 1; while($k < 4) { $prod_item_num = "\$item" . $k; $unique_query1 = "SELECT * FROM product_db WHERE id = $prod_item_num"; echo $unique_query1; echo "<br />"; $k++; } ?> The above displays the following SELECT * FROM product_db WHERE id = $item1 SELECT * FROM product_db WHERE id = $item2 SELECT * FROM product_db WHERE id = $item3 I need the $item(1-3) values to echo what I made them. I've tried many different ways, none of which worked. Quote Link to comment https://forums.phpfreaks.com/topic/268313-item1-3-not-displaying-defined-value/ Share on other sites More sharing options...
Seancon1 Posted September 12, 2012 Author Share Posted September 12, 2012 Solved I found the solution, thank you for looking at my post anyways. <?php $items = array ( "1" => $item1, "2" => $item2, "3" => $item3 ); $items1 = $items['1']; echo "The item has been displayed \$items1 = $items1 <br />"; $k = 1; while($k < 4) { $prod_item_num = $items["$k"]; $unique_query1 = "SELECT * FROM product_db WHERE id = $prod_item_num"; echo $unique_query1; echo "<br />"; $k++; } ?> Displays: The item has been displayed $items1 = one SELECT * FROM product_db WHERE id = one SELECT * FROM product_db WHERE id = two SELECT * FROM product_db WHERE id = three Quote Link to comment https://forums.phpfreaks.com/topic/268313-item1-3-not-displaying-defined-value/#findComment-1377463 Share on other sites More sharing options...
Pikachu2000 Posted September 12, 2012 Share Posted September 12, 2012 Don't run queries in loops. Get all of the data in one query. $items = array( 'one', 'two', 'three' ); $values = implode( "', '", $items ); $unique_query1 = "SELECT * FROM product_db WHERE id IN ( '$values' )"; Quote Link to comment https://forums.phpfreaks.com/topic/268313-item1-3-not-displaying-defined-value/#findComment-1377464 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.