Jump to content

Setting query result into string variable to be used in another query


Recommended Posts

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!

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.

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.

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.