SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: MySQL Question

  1. #1
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - i have a MySQL database called Pages

    in it, there are 4 fields...

    page
    dateadded
    text
    author

    how would i get it so that if i typed in misc.php?page=legal and it would find the record with legal in the page column, and then display the dateadded and author below it

    i'm only a beginner...so be nice to me

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this, assuming you were already connected:
    Code:
    if($page == "legal") {
    $results = mysql_query("select * from Pages where page = 'legal';");
        while($row = mysql_fetcharray($results) {
            echo "Lalalala $row['page'] is the page title";
    }
    A bit rusty on my PHP but that's the basic outline.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need something like this:
    Code:
    <?php
    mysql_connect("server", "user", "pass");
    mysql_select_db("db");
    if ($page == "legal") {
    $query = mysql_query("SELECT * FROM pages WHERE page='$page'");
    $record = mysql_fetch_array($query);
    echo("<table><tr><td>Page</td><td>" . $record["page"] . "</td></tr><tr>Date</td><td>" . $record["dateadded"] . "</td></tr> --repeat that code for the other things you want to display-- </table>";
    }
    <edit>
    darn I see Quinn beat me to it while i was typing the reply
    </edit>

  4. #4
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...whats wrong with the code? i get parse error on line 12

    Code:
    <HTML>
    <HEAD>
    <TITLE>Title</TITLE>
    </HEAD>
    <BODY>
    <?php
    mysql_connect("localhost", "root", "xxxxxxxx");
    mysql_select_db("misc");
    if ($page == "legal") {
    $query = mysql_query("SELECT * FROM page WHERE page='$page'");
    $record = mysql_fetch_array($query);
    echo("<table><tr><td>Page</td><td>" . $record["pagetext"] . "</td></tr><tr>Date</td><td>" . $record["dateadded"] . "</td></tr></table>";
    }
    ?>
    </BODY>
    </HTML>

  5. #5
    SitePoint Evangelist mad-onion's Avatar
    Join Date
    Aug 2000
    Location
    Land of the long white cloud
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Line 12

    echo("<table><tr><td>Page</td><td>" . $record["pagetext"] . "</td></tr><tr>Date</td><td>" . $record["dateadded"] . "</td></tr></table>";

    should read

    echo "<table><tr><td>Page</td><td>" . $record["pagetext"] . "</td></tr><tr>Date</td><td>" . $record["dateadded"] . "</td></tr></table>";

    spot the difference

    Hope that helps!
    SiteOptions >> Services :: Products :: Contact
    Developers of PHP, C++, Visual Basic, MySQL, and more!

  6. #6
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...another problem

    i'm designing a content management system now, so that i dont need to upload the files...but i need to add the if (page == $page) (quick guess ) to it to make it show up

    i.e if i add misc.php?page=jokes then i need the

    Code:
    if ($page == "jokes") {
    $query = mysql_query("SELECT * FROM page WHERE page='$page'");
    $record = mysql_fetch_array($query);
    echo "<center><font=verdana><size=2><b>" . $record["pagetext"] . "<br><br>Date Added:" . $record["DateAdded"] . "<br><br>Generated via AtomicEngine 1.1" ;
    to be added to the misc.php file

    any ideas

    also, qslack, james and mad-onion...do you want a mention in my sites credits...this is the engine that powers the whole site and you really are helping!

    Alex

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you need to add another if statement for each page you want to display, eg:
    Code:
    if ($page == "helllo") {
    //code for hello page
    }
    if ($page == "goodbye") {
    //code for goodbye page
    }
    But on your joke example, the code within the if statement needs to be modified as well to display jokes rather than something else. Do you need help with that?

  8. #8
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I've just been told that I misunderstood the question so I'm gonna have another go
    Code:
    mysql_connect("localhost", "root", "pass");
    mysql_select_db("db");
    $query = mysql_query("SELECT * FROM page WHERE page='$page'");
    $record = mysql_fetch_array($query);
    echo "<center><font=verdana><size=2><b>" . $record["pagetext"] . "<br><br>Date Added:" . $record["DateAdded"] . "<br><br>Generated via AtomicEngine 1.1" ;
    OK I haven't tested that but it should work. Let me know if there are any parse errors and I'll fix it asap
    <Edited by James on 01-01-2001 at 05:05 AM>

  9. #9
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...error

    bear in mind i have it in a template, so its a few lines extra

    Fatal error: Call to undefined function: mysql_select() in c:/blds/htdocs/misc.php on line 224

    you can also see its a win32 server

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry bout that Alex, mysql_select should be changed to mysql_query.
    I've fixed it in my original post as well
    (thats what happens when i've stayed up too late for a new years party )

  11. #11
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm..'nother MySQL question:

    Code:
     <?php
    else:
    
    mysql_connect("localhost", "root", "oblivion");
    mysql_select_db("misc");
    mysql_query("INSERT INTO pages(page, Author, DateAdded, pagetext) VALUES ('$page', '$name', 2001020, '$pagetext')")
    
    endif;
    
    ?>
    all the variables come from a form that is on the same page (hence the else statement)

    Alex

    Thanx James

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do it like this:
    Code:
    mysql_query("INSERT INTO pages SET page='$page', Author='$author', DateAdded='20001020', pagetext='$pagetext'");


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •