Got some questions about a database script

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Got some questions about a database script

Post by JKM »

Here's the code I use:

Code: Select all

<body>
<?
    mysql_connect('localhost','dvddb','xxx');
    @mysql_select_db('dvddb') or die("Can't find database");
    $sel = mysql_query("SELECT * FROM dvd");
    $num_rows = mysql_num_rows($sel);
    echo mysql_error();
    $result = mysql_query("SELECT * FROM dvd ORDER BY id DESC");
    if (isset( $_GET['order'])) {
        $page = $_GET['order'];
    }
    else {
        $page = "?";
    }
        
    switch ($page) {
        default:
            $result = mysql_query("SELECT * FROM dvd ORDER BY id DESC");
        break;
        case "oldest":
            $result = mysql_query("SELECT * FROM dvd ORDER BY LCASE(oldest) DESC, id ASC");
        break;
        case "rate":
            $result = mysql_query("SELECT * FROM dvd ORDER BY LCASE(rate) ASC, rate DESC");
        break;
        case "director":
            $result = mysql_query("SELECT * FROM dvd ORDER BY LCASE(director) ASC, rate DESC");
        break;
        case "genre":
            $result = mysql_query("SELECT * FROM dvd ORDER BY LCASE(genre) ASC, genre DESC");
        break;
        case "page":
            $result = mysql_query("SELECT * FROM dvd ORDER BY id DESC LIMIT " . $_GET['start'] .",25");
            $last = ($_GET['start']-25);
            if ($last < 0) $last = 0;
        break;
    }
    while ($myrow = mysql_fetch_array($result)) {
        if (isset($_GET['order'])) {
            if ($_GET['order'] == "oldest") {
                if (strcasecmp($myrow['id']) != 0) {
                    echo $myrow['id'];
                }
            }
            if ($_GET['order'] == "rate") {
                if (strcasecmp($myrow['rate']) != 0) {
                    echo $myrow['rate'];
                }
            }
            if ($_GET['order'] == "director") {
                if (strcasecmp($myrow['director']) != 0) {
                    echo $myrow['director'];
                }
            }
            if ($_GET['order'] == "genre") {
                if (strcasecmp($myrow['genre']) != 0) {
                    echo $myrow['genre'];
                }
            }
        }
    }
?>
<div class="dvd">
    <img class="dvdpic" src="<? echo $myrow['bilde'] ?>.jpg" border="0" alt="<? echo $myrow['title'] ?>" />
    <h2><? $myrow['title'] ?></h2>
    <p><span class="what">Added:</span> <? echo $myrow['added'] ?></p>
    <p><span class="what">Director:</span> <? echo $myrow['director'] ?></p>
    <p><span class="what">Genre:</span> <? echo $myrow['genre'] ?></p>
    <p><span class="what">Rate:</span> <img src="<? echo $myrow['rate'] ?>.jpg" border="0" alt="<? echo $myrow['rate'] ?>" /></p>
    <p><span class="what">IMDb:</span> <a href="http://www.imdb.com/title/<? echo $myrow['imdb'] ?>" target="_blank">IMDb profile</a></p>
<?
    $review = echo $myrow['review'];
    if($myrow['reviewyesno'] == '1'){
        echo '<p><span class=\"what\">Review:</span> <a href=\"$review\">Read review</a></p>';
    }
    else{
        echo '<!-- No review --->';
    }
?>
</div>
</body>
But I've got some problems/questions..
  • How can I let the script make new div's for each row in the db? This only adds one of the time.
  • Does this script show 25 rows only when sorting by id? If so - how to make it shows 25 anytime?
  • I was thinking about making a edit script. So how can I make a input- (and submit-button) where I could write the DVD's ID, and when pressing submit - it should show the row values in input columns.
  • Would that "review script" work?
  • This script I've got, doesn't get data from the datbase. It only works if I put "$result = mysql_query("SELECT * FROM dvd ORDER BY id DESC");" instead of line9 to line37. What should I do?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Got some questions about a database script

Post by califdon »

JKM wrote:
  1. How can I let the script make new div's for each row in the db? This only adds one of the time.
  2. Does this script show 25 rows only when sorting by id? If so - how to make it shows 25 anytime?
  3. I was thinking about making a edit script. So how can I make a input- (and submit-button) where I could write the DVD's ID, and when pressing submit - it should show the row values in input columns.
  4. Would that "review script" work?
  5. This script I've got, doesn't get data from the datbase. It only works if I put "$result = mysql_query("SELECT * FROM dvd ORDER BY id DESC");" instead of line9 to line37. What should I do?
The first thing you should do is learn some basics of PHP.
  1. Put the HTML for the divs inside the while loop.
  2. Why are you asking us? What does it do on your computer?
  3. I think you better learn some more PHP before thinking about more complicated scripts.
  4. I don't understand what you are asking.
  5. The script certainly does get data from the database. That's what it's all about. mysql_query() is the PHP function that gathers data from the database. If you're not using it in the right place, that's an issue of how you organize your code logic.
If, as I suspect, you got a script from somewhere and are trying to modify it to meet your needs, please be advised that this is not an effective way to learn how to write PHP code, and asking broad questions in a forum when you don't have some understanding of the language or of coding techniques isn't going to get you very far, either. I strongly recommend that you read some online tutorials (start with http://w3schools.com) and begin with simple scripts, not complex ones. After you get a few simple scripts to work, THEN you can move on to more complicated scripts.

Good luck.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Got some questions about a database script

Post by JKM »

Thanks for the tips!

I'm learning alot from reading other scripts. That's how I learned xHTML/CSS. I've also learned alot from php.com when errors occured. Do you know about other scripts that is similar to what I'm trying to do?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Got some questions about a database script

Post by califdon »

JKM wrote:Thanks for the tips!

I'm learning alot from reading other scripts. That's how I learned xHTML/CSS. I've also learned alot from php.com when errors occured. Do you know about other scripts that is similar to what I'm trying to do?
On a scale of 1 to 10, learning programming by reading other people's scripts ranks about -8. That's because (1) you don't know whether the script you're "learning" from is well-written (quite often they are not); (2) you won't understand why something is written the way it is (it is very common to use a particular syntax because of a special situation that you are unlikely to recognize); (3) all programming is based on logic that is never obvious when you just read through a script. You absolutely need to learn certain basic principles first, before you stumble through code that only applies to the particular situation in that script. You may think you're learning a lot, but what you're really learning is mostly going to make it that much harder to actually learn to do it yourself. Hey, it makes no difference to me what you do, but I sincerely recommend most strongly that you do not continue trying to learn in the way that you are trying to do. Believe me, it will only hold you back. Learn basic principles first!. There are lots and lots of tutorials online. Start with them. Use Google and search for things like php tutorial, mysql tutorial, javascript tutorial.
Post Reply