php_novice wrote:
Code: Select all
<?
// select all records ordered by date
$topStory = mysql_fetch_array(mysql_query("Select * FROM tble ORDER BY id LIMIT 6"));
$topHead = $topStory['headline'];
?>
<body>
<? echo $topHead['0']; ?><br />
<? echo $topHead['1']; ?><br />
<? echo $topHead['2']; ?><br />
<? echo $topHead['3']; ?><br />
<? echo $topHead['4']; ?><br />
<? echo $topHead['5']; ?><br />
Couple of things I suspect:
1. Are you getting any results from your query?
Try:
Code: Select all
<?php
print mysql_num_rows(mysql_query("Select * FROM tble ORDER BY id LIMIT 6"));
?>
You're looking for a value greater than 0. If it's 0 (or errors), there is a problem with your query or no data in the table.
2. Not all php configurations support short tags. It's best practice to use '<?php' and '?>' instead of '<?' and '?>'
3. The code (the way you have it) will only return an array from 1 row of data. It looks like you want 6 different headlines, so you need to do something like this:
Code: Select all
<?php
$topHead = array();
$res = mysql_query("Select * FROM tble ORDER BY id LIMIT 6");
while($topStory = mysql_fetch_array($res)) {
$topHead[] = $topStory['headline'];
}
// Finally, call each iteration of the $topHead array
echo $topHead[0];
echo $topHead[1];
echo $topHead[2];
echo $topHead[3];
echo $topHead[4];
echo $topHead[5];
?>