0

I store my testimonials in a database table and would like them to be displayed on my website via this for loop:

<?php

$count = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) cnt FROM testimonials"));

for ($i = 1; $i <= intval($count['cnt']); $i++)
{
    $sql = mysql_query("SELECT * FROM testimonials WHERE id='{$i}'");
    ?>
    <li class="span4">
        <div class="thumbnail thumbnail-1">
            <section> 
                <a class="link-1" style="cursor:pointer;"><?php echo $sql['name'] ?></a>
                <p><?php echo $sql['text'] ?></p>
                <a href="http:// <?php echo $sql['product'] ?> /" class="link-1"><?php echo $sql['product'] ?></a>
            </section>
        </div>
    </li>

    <?php
}

?>

The issue is that the $sql variables product, name and text are not displaying. However the $count is getting the correct intval, so it knows there are entries.

It's also worth pointing out that the loop is working as I get the <li> <div> and <section> tags working, the only issue is the <a>'s and the <p> not getting the textual value from the echo

P.S. I know that mysql_* functions are depreciated however my php version 5.3 and they are only depreciated from 5.5 so they are ok for my website.

3
  • 1
    You already got answer, but... i would really solve this on another way. You can have too many queries in loop, and it could seriously affect performance... Commented Jul 6, 2013 at 10:48
  • explain how you would do it Commented Jul 6, 2013 at 10:51
  • the $sql query has to be done in the loop. Commented Jul 6, 2013 at 10:52

1 Answer 1

2

you missed to fetch second sql

add this line

   $result= mysql_fetch_assoc($sql) ;

and then call your variables like that

 <?php echo $result['name'] ; ?>
 <?php echo $result['product'] ;?>
 <?php echo $result['product'] ; ?>
                              ^^-----dont forget `;` because you missed them also
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried this before and it gave me an error on the page. I will try again and post what is displayed on the page
Nevermind, it worked thanks a lot. When I get the change I will accept the answer.
I said about mysql_* in the question, please read all of it before saying about it.
Oh btw, when you do <?php ?> for 1 line, the ; is automatically added and it not needed

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.