Jump to content


This topic is now archived and is closed to further replies.


Some help with sql_query

Recommended Posts

Ive been using php for a while now, but I am still new at MySQL. I had a question about how to retrieve data from a sql table. i believe I use the sql_query to do it but Im not sure. Below is my example.

[*]-ppl submit art and info with it
-it stored to Mysql under the table "art" with a row called medium (photo, draw, cg)
-there is a page that pulls up this art called "art.php"
-if you goto the photography page (art.php?medium=photo) it shows only photos
-if you go to drawing ect ect.
-how do i make it grab only the art i request instead of the entire art table?
-also how do I go about making a flexible html table to desplay this info?
-is it hard to do pagination with all this?

Thanks for the help, someone always comes through and hits the nail on the head.

Share this post

Link to post
Share on other sites
I may be able to answer a few of those questions...

If each row has a unique number tied to it in MySQL called a primary key, you can use that to easily and safely display an individual row.

Example: <a href="art.php?photo=medium&photoID=1">Click for medium photo</a>
You can write a simple conditional or switch() to get it to display what you're looking for (I prefer switch).

This is just a simple guestimation of what to do.

switch ($_GET['Photo') {
    echo 'whatever it is you want to be displayed by default.';

  case 'medium';
    // If the user has opted to select an individual photo, store the WHERE clause in a variable so we don't have to double up on our work.
    if ($_GET['photoID']) {
      $PhotoConditional = 'WHERE photo_id = $_GET[photoID]';
    $SQL_Photo = MySQL_Query("SELECT photo FROM art $PhotoConditional");
    while ($r = MySQL_Fetch_Array($SQL_Photo)) {
        echo $r['photo'].'<br />';

The above switch() should in theory display either all data or a single photo.  If you want all, just don't include the photoID in the URL.

Pagination is an entirely and painful subject.  I recommend going into the tutorials on this website and read the one there to get a better understanding of what to do.

Share this post

Link to post
Share on other sites
art table
art_id (PK)
medium (= 'photo', 'draw' etc)

If user selects photo page

SELECT * FROM art WHERE medium = 'photo'

Share this post

Link to post
Share on other sites
The pagination is really the hardest part of all this... you have run some fancy SQL to get the entire result set count efficiently.

Share this post

Link to post
Share on other sites
Thx Barand for helping me on my table layout and gijew for the sample code. I tried it our and its seems to be working pretty good. Ill do some altering and send a link so that you guys can check it out when its done. THX!

Share this post

Link to post
Share on other sites


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.