SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how can i construct this call to my database

    Hi,

    I have this problem that i dont know how to work out as im still very new to php/sql. I have a script that queries my database and pulls out information about a blog entry ( title, body text, author, date submitted). All this info is generated from one table ( and yes the author column is creating redundant data but ill sort that out soon)

    I have another table in my db thats for categories. My first query gets the category id fine and assigns it to a variable ($cat). Now the problem question i have is how to incorportae the query ive made below. Can i include it somehow in the query above and do both at once or what? ive never made a call to two databases in the same instance before.

    Could anyone suggest a good way to do this? As the code is now all thats getting printed out( in green) is resource1d#4???!?!?!

    PHP Code:
    <ul>
        


    <?php 
    require_once ('mysql_connect.php'); // Connect to the db.
            
    $sql "SELECT * FROM entries ORDER BY date_submitted DESC LIMIT 5";


    $query mysql_query($sql);
    echo 
    $query;

    while (
    $row mysql_fetch_array($query)) {
      
    $title $row['title'];
        
    $body $row['body'];
        
    $author $row['author_id'];
        
    $date $row['date_submitted'];
        
    $cat $row['category_id'];

        
    ?>





    <li><?php echo $title; echo $date; echo $body?></li>
    <p style="red"><?php echo $author?></p>
    <p style="color: red;"><?php echo $cat?></p>

    <?php
    }
    ?>


    <?php

    $sql 
    "SELECT category FROM categories Where categories.category_id = $cat";
    $result mysql_query($sql);

    ?>

    </ul>

    <p style="color: green;"><?php echo $result?>
    </p>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    what you're looking for is called a join -- every basic sql tutorial will cover this topic
    Code:
    SELECT entries.* 
         , categories.category
      FROM entries 
    INNER
      JOIN categories
        ON categories.category_id = entries.category_id
    ORDER
        BY entries.date_submitted DESC LIMIT 5
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the reply that worked a treat! I really should have realised thats what i needed there!


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
  •