Query help?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_novice
Forum Newbie
Posts: 2
Joined: Wed Apr 16, 2008 7:54 am

Query help?

Post by php_novice »

Hello,

I was wondering if someone could help me with some code. I want to simply call out a headline from my database. The first, then the second etc...

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 />
 
Howeverm it pulls out the first headline letter, then the second! Can anyone help me. I'd really like to do it like this and not in a while loop.

Thank you
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Query help?

Post by aceconcepts »

What happens if you remove the single quotes:

Code: Select all

 
# <? echo $topHead[0]; ?><br />
# <? echo $topHead[1]; ?><br />
# <? echo $topHead[2]; ?><br />
# <? echo $topHead[3]; ?><br />
# <? echo $topHead[4]; ?><br />
# <? echo $topHead[5]; ?><br />
 
php_novice
Forum Newbie
Posts: 2
Joined: Wed Apr 16, 2008 7:54 am

Re: Query help?

Post by php_novice »

Nothing, still does the same thing:-(
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Query help?

Post by aceconcepts »

instead of echoing the value try printing it as an array e.g.

Code: Select all

 
print_r($topStory);
 
And then post it in the forum.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Query help?

Post by flying_circus »

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];
?>
Post Reply