Jump to content

Php Query Help


driz

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

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

Maybe the && comparator has precedence over the = operator.Try this:while( ($testimonials_row = mysql_fetch_array($testimonials_result)) && $i < 3)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...