Jump to content

Getting x rows from a database


sidorak95

Recommended Posts

Hi,

 

I'm trying to make an art portfolio, and I'm trying to get the images from a mysql database. However, I would like the portfolio to be paged. The template I'm using by default has pages. However, it's entirely HTML.

 

I'm looking to:

-Grab x rows from database (in my case, 3)

-Echo the 3 rows

-Then echo the HTML code that would create a new page

-Repeat until no more entries

 

Can you do this, and if so, how? I know about pagination, but I think this approach is much simpler and I would prefer my approach.

 

Thanks.

Link to comment
Share on other sites

I'm sorry, I didn't see anywhere where it prohibited you from posting simple php questions. ^^

 

I am familiar with getting data from a database, and I was thinking of using limit, but in my tests it failed.

 

I also fail to see the relevance of echo vs. print. While I did say I would echo something, I won't necessarily use echo and that's really not my main concern right now.

Link to comment
Share on other sites

This is what I was trying with limit:

<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("a8784hos_gpt") or die(mysql_error());

$result = mysql_query("SELECT * FROM test LIMIT 1, 3") 
or die(mysql_error());  
$count = 0;

while($row = mysql_fetch_array( $result )) {
while($count < 3){
echo $row['Name'] . '<br>';
$count = $count + 1;
}
echo '<hr>';
} 

?>

However, it failed, some I'm back to square one:

<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("a8784hos_gpt") or die(mysql_error());

$result = mysql_query("SELECT * FROM test") 
or die(mysql_error());  

while($row = mysql_fetch_array( $result )) {
echo $row['Name'];
} 

?>

Link to comment
Share on other sites

Well the purpose of limit is used when you don't want ALL of the records returned from the query. Say for example you had a query as such:

 

$query = "SELECT * FROM fruits WHERE taste = "good";

 

And inside the fruits table you have:

 

name taste

apple                good

banana            good

peach              good

 

With LIMIT (x) at the end of the query it would only show x amount or rows so if you put LIMIT 2 it would only return apple and banana

Link to comment
Share on other sites

Oh god...

 

Query like normal, but apply a LIMIT clause to your query. You want to query out 3 rows at a time.. So you'd have a query like...

 

SELECT * FROM tableName LIMIT 0,3 

 

 

that will query the first 3 recent rows in your DB (you could apply an ORDER BY [DESC|ASC]). You can elaborate further on the 0,3 and apply them by a variable. $x, $y for example.. and make those variables dynamic based on a page variable or something where page 2 would show 4,6 which will show rows 4 through 6.. I'm sorry I'm not being super nondescript and I am rushing through this a bit as I run out the door so to speak but I wanted to at least give the impression of what you can try

 

Link to comment
Share on other sites

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.