Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/268313-item1-3-not-displaying-defined-value/
Share on other sites

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

 

 

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' )";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.