-4

Firstly I am brand new to PHP and MySQL.

My goal is to display testimonies from students that are in the database. I realize that my code is probably WAYYY overcomplicated, but any help is much obliged. Currently, It is displaying the same result. I think it's because I call for the same thing. Idk I am very confused. Help please!!

<div class="block_main_testimonials">
    <h4>Student Corner</h4>
<?php   
// Connect to the file above here   
require "includes/connect.php";  

$data = mysql_query("SELECT * FROM `testimonials` ORDER BY RAND() LIMIT 0,1") 
or die(mysql_error()); 

$info = mysql_fetch_array( $data ) ; 

?>
                        <div class="one_half">
                            <div class="block_testimonials_2">
                                <div class="content">
                                    <div class="text">
                                        <p>“<?php echo $info['quote'] ;?>”</p>

                                        <div class="tail"></div>
                                    </div>

                                    <div class="author">
                                      <p><?php echo $info['name'] ; ?>,<span class="position"> <a href="/<?php echo $info['url']" echo $info['department']; ?</a></span></p></div>
                                </div>
                                <div class="clearboth"></div>
                            </div>
                        </div>

1 Answers1

0

I'm not sure what are you trying to do, but you don't want to use the GROUP BY clause.

  • If you want to select all entries, use:

    mysql_query("SELECT * FROM `testimonials`");
    
  • If you want to select one random entry, use:

    mysql_query("SELECT * FROM `testimonials` ORDER BY RAND() LIMIT 1");
    

    and get rid of the while loop:

    $info = mysql_fetch_array($data);
    
  • If you want to select a certain number of random entries, use:

    mysql_query("SELECT * FROM `testimonials` ORDER BY RAND() LIMIT 2");
    

    Where 2 is number of results you want to recive. This will never return one row twice unless you have the exact same row stored twice in the database.

  • If you want to select all entries in random order, use:

    mysql_query("SELECT * FROM `testimonials` ORDER BY RAND()");
    

But please, if you're starting to learn PHP, don't use the mysql_* functions. Use mysqli or PDO instead.

Community
  • 1
  • 1
Petr R.
  • 1,247
  • 2
  • 22
  • 30