0

Basically I have a query called using PHP:

<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");


while ($row = mssql_fetch_array($result)) { 

if ($row['ColourID'] == "1") {

    $sayclass1="imgactive";

    }else{

    $sayclass1="imginactive"; }


            ?>

As you can see once I execute the query I then loop it, the problem is that it returns an array, now in some instances I need to use the full array, but I would like to be able to select one entry from it for if statements and such. For example:

<img id="h" src="<?php echo $row['thumbimg']; ?>

now thumbimg is a column in my DB, and it just holds a url. However due to the fact its an array the picture doesn't display because its echoing all the values, so instead of images/image1.png for example it is echoing images/image1.png images/image2.png images/image3.png etc etc...

I hope that makes sense, and can anyone tell me how to manipulate the query/code slight to still return all the entries but to select certain values from the arrays please?

4
  • You need to select one of the fetched rows and use that as you say with $row['thumbimg']; please check this link: php.net/manual/en/function.mysql-fetch-row.php Commented Oct 12, 2012 at 10:04
  • 3
    BTW, you have a typo if ($row['ColourID'] = "1"). = should be ==. Commented Oct 12, 2012 at 10:04
  • Unable to decode Your question :( Commented Oct 12, 2012 at 10:06
  • Thanks you for the typo comment, and im using MS SQL not My SQL. Commented Oct 12, 2012 at 10:10

2 Answers 2

1

You need to use the img tag with in for each if you want to show all the images

foreach($row[thumbimg] as $img):
<img id="h" src="<?php echo $img; ?>
end foreach;
Sign up to request clarification or add additional context in comments.

Comments

0
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' ");


while ($row = mssql_fetch_array($result)) { 

     if ($row['ColourID'] == "1") {

        $sayclass1="imgactive";

    }else{

        $sayclass1="imginactive"; 
    }
 echo '<img id="h" src="'.$row['thumbimg'].'">';
}

?>

Using this above code, you will get one image per line from the database. The part of code you have copy-pasted is not enough to determine where your mistake is and why $row['thumbimg'] contains a concatenated value of the result.

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.