Jump to content

Problem with mysql_fetch_array rows


gli

Recommended Posts

Hi!

I've sorted latest news from my database by id.

Then the results i fetched array.

All is ok.

Then i want to show recent subject with news text in main news box, but other last two subjects i want to show in archive news box.

And here's my problem that i dont know how to make example third row from that fetched array.

 

Sorry for my bad English.

 

And this is the code:

 

<?

// build the query, order by newest ID and limit it to 10
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3";

// run the query and set a result identifier to the recordset
$res = mysql_query($sql);


$article = mysql_fetch_array($res);

[color=red]PROBLEM
echo $article['subject'] . $article['newstext']  ; // there is information from first row
// there is some html elements
echo $article['subject']; //  there is information from second row
// some other html elements
echo $article['subject']; // and there is information from third row
PROBLEM[/color]?> 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/
Share on other sites

Try this?  I am not sure if this is what you want...

 

<?php

// build the query, order by newest ID and limit it to 10
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3";

// run the query and set a result identifier to the recordset
$res = mysql_query($sql);


while($article = mysql_fetch_array($res)) {


echo $article['subject'] . $article['newstext']  ; // there is information from first row
// there is some html elements
echo $article['subject']; //  there is information from second row
// some other html elements
echo $article['subject']; // and there is information from third row
}
?> 

 

Question, in the comments you say limit the query to 10 items, but you have a 3?

you need a while loop:

<?php
// build the query, order by newest ID and limit it to 10
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3";

// run the query and set a result identifier to the recordset
$res = mysql_query($sql);


while ($article = mysql_fetch_assoc($res));

echo $article['subject'] . $article['newstext']  ; // there is information from first row
// there is some html elements
echo $article['subject']; //  there is information from second row
// some other html elements
echo $article['subject']; // and there is information from third row
?> 

thanks for answers, but problem is not solved. i already tried this. this makes that all that three elements repeats each time and there is more than three results.

i need one result with  subject and news text

second only with subject

and third too only with subject

and not in list but in unique places.

 

Through a variable (e.g. i) in there.

 

 

 

<?php

// build the query, order by newest ID and limit it to 10
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3";

// run the query and set a result identifier to the recordset
$res = mysql_query($sql);


$i = 1;
while($article = mysql_fetch_array($res)) {

if($i == 1) {
echo $article['subject'] . $article['newstext'];
}
else {
echo $article['subject'];
}
$i++;
}
?> 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.