Jump to content

Php Query Help


driz
 Share

Recommended Posts

Hi, please look at the following code!Using this same code as a basis, their are three things I'd like to develop, to only show 1 row from the DB, and 2nd be able to show only the most recent row, and lastly how to randomize a row to be shown. Thanks. <?php // connect to the database $host="localhost"; $username="u0558234"; $pass="26jun87"; mysql_connect($host,$username,$pass); mysql_select_db($username); // select everything from the news table $testimonials_query="select * from testimonials"; $testimonials_result=mysql_query($testimonials_query); while($testimonials_row = mysql_fetch_array($testimonials_result)) { echo "<blockquote>"; echo "<p>".$testimonials_row['body']."</p>"; echo "<p class=\"author\">".$testimonials_row['author']."</p>"; echo "</blockquote>"; } // disconnect from the database mysql_close(); ?>

Link to comment
Share on other sites

To only show 1 row, remove the while loop. Replacing it with an if statement would work. To show the most recent, change the query to order rows by whichever field is chronological. To show a random row, change the query to order by a random number. e.g.:select * from testimonials order by RAND() limit 1

Link to comment
Share on other sites

Would I just replace the word While with If? OR am i off base here?However is the a suitable method of using a numerical value as the query to select how many i want to show, so for example 3?

Edited by driz
Link to comment
Share on other sites

This is what I have got:

					$testimonials_query="select * from testimonials";					$testimonials_result=mysql_query($testimonials_query);										$i = 0;					while($testimonials_row = mysql_fetch_array($testimonials_result) && $i < 3) {						echo "<blockquote>";						echo "<p>".$testimonials_row['body']."</p>";						echo "<p class=\"author\">".$testimonials_row['author']."</p>";						echo "</blockquote>";						$i++;					}

Its showing 3 blockquotes meaning its working, but its no longer showing the content? Is their an error somewhere? :/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...