sidorak95 Posted June 24, 2011 Share Posted June 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/ Share on other sites More sharing options...
Xtremer360 Posted June 24, 2011 Share Posted June 24, 2011 So you posted this in a php forum and dealing with something as simple is this is php 101. My recomentdation is opening up a php book and having a look at simple queries and while loops and echo vs. print Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234113 Share on other sites More sharing options...
sidorak95 Posted June 24, 2011 Author Share Posted June 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234119 Share on other sites More sharing options...
Xtremer360 Posted June 24, 2011 Share Posted June 24, 2011 If you include what code you have no we can better help you. Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234121 Share on other sites More sharing options...
sidorak95 Posted June 24, 2011 Author Share Posted June 24, 2011 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234123 Share on other sites More sharing options...
Xtremer360 Posted June 24, 2011 Share Posted June 24, 2011 Try this: while($row = mysql_fetch_array( $result )) { echo $row['Name'] . '<br>'; echo '<hr>'; } Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234126 Share on other sites More sharing options...
sidorak95 Posted June 24, 2011 Author Share Posted June 24, 2011 Thank you, however, it doesn't solve my problem. I'm looking to echo 3 rows, then some HTML code, and then resume. This won't resume fetching from the database where we left off, so I feel limit is the wrong way to go. Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234130 Share on other sites More sharing options...
Xtremer360 Posted June 24, 2011 Share Posted June 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234134 Share on other sites More sharing options...
monkeytooth Posted June 24, 2011 Share Posted June 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234140 Share on other sites More sharing options...
wildteen88 Posted June 24, 2011 Share Posted June 24, 2011 The technique for creating pages from your query results is called pagination. Have a read through this tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/240263-getting-x-rows-from-a-database/#findComment-1234437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.