Jump to content

Recommended Posts

Hi

 

How do I limit the results returned by php from my database??

 

I.e. I have a table of articles, and I want to display the newest 5 articles on my homepage...

 

How do I do that?

 

This is the code I have so far... but this displays every record ...

 

<?php
$con = mysql_connect("CONNECTION DETAILS");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}mysql_select_db("bssql", $con);$result = mysql_query("SELECT * FROM articles ORDER BY ID DESC");while($row = mysql_fetch_array($result))
  {
   echo $row['Title']; 
   echo $row['SubTitle']; 
   echo "<br />";
  }mysql_close($con);
?>

 

Thanks in advance ..

 

Gem

 

PS: Can you tell me exactly where I need to add something if necessary ... I always mess things up otherwise  :)

Link to comment
https://forums.phpfreaks.com/topic/149035-solved-how-do-you-limit-results/
Share on other sites

  <?php

$con = mysql_connect("CONNECTION");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

}mysql_select_db("bssql", $con);$result = mysql_query("SELECT title, subtitle FROM articles ORDER BY ID DESC LIMIT 2");while($row = mysql_fetch_array($result))

  {

    echo $row['Title'];

    echo $row['SubTitle'];

       

  }mysql_close($con);

?>

 

Had to do 2 as theres only 3 records in the table lol ... doesnt do anything though ... all i get is a blank page ... I think I messed up the code ... the query works in SQLyog ...?

<?php
$con = mysql_connect("CONNECTION");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);
$result = mysql_query("SELECT title, subtitle FROM articles ORDER BY ID DESC LIMIT 2") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    echo $row['Title']; 
    echo $row['SubTitle'];
}
mysql_close($con);
?>

 

try this..

 

all i can add like i said it already perfect.

<?php
$con = mysql_connect("CONNECTION");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);
$result = mysql_query("SELECT `title`, `subtitle` FROM `articles` ORDER BY `ID` DESC LIMIT `2`") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
    echo $row['Title']; 
    echo $row['SubTitle'];
}
mysql_close($con);
?>

red I copied your code and it didnt like the LIMIT '2' , came back with an error ...

 

so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content ....

 

 

red I copied your code and it didnt like the LIMIT '2' , came back with an error ...

 

so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content ....

 

what error ?

try this and cheek your spelling like uppercase letters are not the same as lowercase letters in php.

 

currently ID << capitalized is it in the database cheek.

<?php
$con = mysql_connect("CONNECTION");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);
$result = mysql_query("SELECT `title`, `subtitle` FROM `articles` ORDER BY `ID` DESC LIMIT 2") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
    echo $row['title']; 
    echo $row['subTitle'];
}
mysql_close($con);
?>

red I copied your code and it didnt like the LIMIT '2' , came back with an error ...

 

so I did your code with LIMIT 2 .. and I've got a blank again ... this is interesting though, without the LIMIT 2 bit, the original code works fine ... with the LIMIT 2 I get no errors, but also no content ....

 

what error ?

 

Caused by the ticks around the 2 in the limit clause. They shouldnt be there

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.