Forum Moderators: coopster

Message Too Old, No Replies

PHP code not retrieving all MySQL records

         

dbarasuk

8:57 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



Hi everyone,

I am just three months old in scripting with PHP and am encountering some problems for which I'd like to request your help.

I have one table called 'articles' in the MySQL database whose goals is to store messages sent by visitors to my website.

Before I deleted offline some records of that table that I had myself inserted using PHP, I could see all the articles showing up on the webpage, but after I deleted some articles from that table, I can see from that table that some records are not showing up any more on the webpage this, both offline and online.

Can somebody tell me the reason why I only can retrieve a part of the recods?

The SQL query code looked like this:
// some content before the sql query (not necessary here)...
// create SQL query
$sql =" SELECT * FROM articles ORDER BY article_id DESC LIMIT 0, 30";
$result = mysql_query($sql, $darconn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if ($_GET &&!$_POST) {
if (isset($_GET['article_id']) && is_numeric($_GET['article_id'])) {
$article_id = $_GET['article_id'];
}
else {
$article_id = NULL; }
}

?>

In the body section the code retrieving the articles with option to update or delete (actually it's my admin page) the records is as follows:

<?php while($row = mysql_fetch_assoc($result)) {?>

<tr>

<td><?php echo $row['name'];?></td>
<td style="padding-left:30px;"><?php echo $row['title'];?></td>
<td style="text-align:left; padding-left:30px;"><?php
$extract = substr($row['content'], 0, 100);
$lastSpace = strrpos($extract, ' ');
if(strlen($row['content']) <= 101) {

echo $row['content'];
}
else {
echo '<p>'.substr($row['content'], 0, $lastSpace).'(<a href="article.php?article_id='.$_GET['article_id'].'">Read More...</a>)</p>';
}
?>
</td>
<td style="padding-left:30px; text-align:left;" ><a href="update_article.php?article_id=<?php echo $row['article_id'];?>">EDIT</a> </td>
<td style=" border-right: 0px ; padding-left:30px; text-align:left; border-right:1px dotted red;" ><a href="delete_article.php?article_id=<?php echo $row['article_id'];?>">DELETE</a></td>
</tr>
<?php }
?>

Any idea of what went wrong preventing me therefore from retrieving all the records?

I'll be very greatful to anybody with the right answer to this problem;
Kind regards,
Dbarasuk B.

jatar_k

2:14 pm on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld dbarasuk,

maybe just try removing the LIMIT clause in your query

from
$sql =" SELECT * FROM articles ORDER BY article_id DESC LIMIT 0, 30";

to
$sql =" SELECT * FROM articles ORDER BY article_id DESC";

I am guessing that should do it. I imagine you didn't notice at first because there were less than 30 results

dbarasuk

2:59 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



Hi jatar_k

Your proposition is a bit magic because it almost solved my problem. Why do I say almost? because the last record in Mysql still does not come to the wepage.

normally, the statement "order by article_id desc" let me expect that it's indeed that last record that would come first on the webpage, however it's exactly that one to be missing.

Any more suggestion, please?

thanks,

D B.

jatar_k

3:05 pm on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you have an extra line in your top chunk of code

$row = mysql_fetch_assoc($result);

so when you start your loop in the lower portion you have already skipped a record

just comment or remove the top one

System

5:15 pm on Jan 15, 2008 (gmt 0)

redhat



The following message was cut out to new thread by eelixduppy. New thread at: php/3548859.htm [webmasterworld.com]
12:28 pm on Jan. 15, 2008 (est -5)