1

I have the following table in mysql. I need to select the Hid(s) from this table and append the results to a string '$s'. It would be great if you could help.

Table name : CASES

Did   Hid   Year  Case
---   ---   ----  ----
 1     1    2011   6
 1     1    2012   7
 2     2    2011   40
 2     2    2012   10

php code segment:

$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
    //I need to append the Hid(s) to a String '$s' declared above
}
1
  • 1
    $s.= $rw["Hid"]; which is shorthand for $s = $s.$rw["Hid"]; Commented Jul 24, 2013 at 5:57

4 Answers 4

1

Assuming your query works, which it looks like it might:

$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
    $s .= $rw['Hid'];
}

That will just give you a string with all of the Hids together.. if you want to have a character in between or something else, you could:

$did=1;
$yr=2011;
$s=array();
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";
$r=mysql_query($q);
while($rw=mysql_fetch_assoc($r))
{
    $s[] = $rw['Hid'];
}
$result = implode( ',', $s );

$result above will end up with a comma-separated list of Hids.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you everyone for posting answers and suggestions for my thread.
0

Use the string concatenation operator:

$s .= $rw['Hid'];

Comments

0

Get/ retrieve the data's to an array and implode it to a string. Refer http://php.net/manual/en/function.implode.php

for implode

Comments

0
$did=1;
$yr=2011;
$s='';
$q="select Hid from CASES where Did=$did and Year=$yr and Case!=0 ";

    $r=mysql_query($q);
    while($rw=mysql_fetch_assoc($r))
    {
        $s .= $rw['hid'] . '<br />';
    }



echo $s;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.