Call me a N00B, but I cant figure out dynamic pages. (PHP)

Some of you are most likely rolling your eyes at a question like this. But I promos you im very good with PHP. I have just been struggling with this one subject.

What I need help with is… well… one of the reasons I am having trouble is because I have no idea what to google to find help with what I need.

But anyways, here is what I need. On a website I have been working on for quite some time I need it set up so if there is a search that returns allot of results it doesn’t print out a page that’s 5 miles long. Lets say I have a user search up something, and it returns 75 results, and I want a max of 30 results per page. SO that would be 30, 30, & 15.

To my understanding something like this would be done with $_GET, am I correct? See, I set up a similar scenario to that just a few nights ago. I just created a test database, filled it with the names of my friends and there ages, set it to auto increment the ID field, then queries the results back. But the problem is that I’m relying on the fact that in that scenario, the loop will always query where the id field is equal to a count variable I have. Ehh, I’m not sure I’m doing the best job at explaining this. Have a look yourselves.

http://codepad.org/QeMFWKzu

Sorry for my code being so sloppy. I was writing it very late at night and was in that “I just want to get this done and go to bed” kind of mood. But hopefully after seeing that what im trying to say makes sense.

It relies on the ID field to function properly. But on my website, I may be dealing with ID fields that range from 10’s, to 1000’s of numbers apart. So unless I was to query every single field on every single Colum every single time a user searched, that wont work.

Geez, even after like 3 or 4 paragraphs I still feel like I did a crappy job explaining this. I hope it made at least a little sense. Feel free to ask about anything that confuses you, which I assume will be allot. Thanks to anyone who can help.

ALSO:
I feel it must be pointed out, just incase. Im dyslexic. I do my best to spell things properly and use proper grammar but I struggle allot of times. I know its not an excuse, but I just think you should be aware of it just incase I made any horrible mistakes while writing this. Haha.

With MySQL, you can limit the number of rows you are returned with the command LIMIT at the end of the query:

21. $sql2 = "SELECT `name`, `id`, `gender` FROM names LIMIT 0,25";

That's where '0' is which row you start at (0 is first) and '25' is how many rows to return. If you want a sort of page system, you can set up a $_GET variable that returns the page number. You can take that (decremented) and multiply that by the number of rows returned to get the row value you'd need to start at on each page.

There's another useful MySQL command you can use, in case you aren't aware, that would be helpful in the case of optimizing search results:

21. $sql2 = "SELECT `name`, `id`, `gender` FROM names ORDER BY `gender` LIMIT 0,25";

If you use it at the MySQL level instead of the while loop, you'd have to set up a while loop after that is something like this:

while($row = mysql_fetch_assoc($query)) { echo "{$row['name']} ..."; }

Hopefully that helps.

Alright sounds good to me! But i have another question. Ive heard allot of talk about php dropping support for MYSQL, and wanting to switch to MYSQLI? Im someone who really hasent ventured outside of anything that comes prepackaged with XAMPP, so why this change and what does it mean?

Ive heard allot of talk about php dropping support for MYSQL, and wanting to switch to MYSQLI?

This does not refer to the database itself, but to the PHP extension.  You should not use the mysql extension (any functions that start with "mysql_") for new code.  They were superseded ten years ago by mysqli.

Especially if you are "just learning" and don't have legacy code to support, go straight to learning MySQLi or PDO.  Both MySQLi and PDO are included by default in current releases of PHP, so there is nothing to install.  I currently use PDO because I find it a bit more convenient to write code for, but each extension has its advantages and the two are more-or-less on even ground.  More information about choosing.

Im still left confused by this. the tutorials i have been watching cant be more then 2 years old, and the man doing them mentioned nothing of this. Ill chalk that up to him just being a bit misinformed but that still leaves me confused as to why the normal mysql extensions would continue to be supported?

They're still supported because there is a ton of old code out there that webmasters don't seem interested in updating.  Likewise, people continue to write tutorials using old code instead of making the effort to switch.  No offense to the PHP community (which I am a part of), but the majority of PHP coders don't learn how to code very thoroughly.  They manage to "get by" because PHP is very fault-tolerant and has such an easy learning curve.

The idea (back in 2004) was to get everyone to switch over a few years, and then drop the old extension.  It fell flat, however.  Nine years later, when they finally deprecated the extension, everyone acted surprised.

The old mysql extension doesn't support all of MySQL's API (for example, no transactions or prepared statements), is inefficient, and —while not really "insecure" in and of itself— makes it very easy to create security holes in code.

You might be interested in PHPTheRightWay.com.  There is a section on interacting with databases, which uses PDO in its examples.

MySQL PHP code may be a bit easier for some and that's probs why he showed that off. Or he just doesn't know and shouldn't make PHP tutorials :')

you are semi correct on that, however the methods between the two are so close that you literally can almost always just add 'i' to the end of 'mysql' in the command string and it will still work. if i remember correctly, mysqli is an enhanced version of mysql functions.

sleep(1);

Why do you have the code sleeping for a second? Surely you want the web page to load as quickly as possible, not delay it by a seconds from the get go?

$sql2 = "SELECT `name`, `id`, `gender` FROM names WHERE id = $count";

This code is susceptible to malicious code injection. I'd recommend using real_escape_string() or sprintf

$sql2 = sprintf("SELECT `name`, `id`, `gender` FROM names WHERE id = %d",$count);

$connect = array('username'=>'root', 'host'=>'127.0.0.1', 'password'=>'');

Using root is really bad practice. Create an appropriate account with only SELECT and maybe INSERT capabilities within the appropriate database. Side note, is $connect such an appropriate name?for your array?

config.inc.php $dbuser = "db_user"; // database username

$dbpass = "RCPQzNTxMsD5Fh8t"; // database password

$dbname = "db_name"; // database name

$dbhost = "db_host"; // database host

page.php

include_once("config.inc.php");

$link = mysql_connect($dbhost, $dbuser, $dbpassword);


if( $link ){

// process successful db connection

} else {

 // don't print error message, log/email the error.

}

What I need help with is… well… one of the reasons I am having trouble is because I have no idea what to google to find help with what I need.

But anyways, here is what I need. On a website I have been working on for quite some time I need it set up so if there is a search that returns allot of results it doesn’t print out a page that’s 5 miles long. Lets say I have a user search up something, and it returns 75 results, and I want a max of 30 results per page. SO that would be 30, 30, & 15.

I realize this post is old... but to help OP (or anyone who stumbles across this post from a Google Search)... what the OP is asking about is called "pagination". He just didn't know the term for it.

A simple Stack Overflow Question/Answer on the subject: http://stackoverflow.com/questions/3705318/simple-php-pagination

Thanks,
Brian