SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot impunjabians's Avatar
    Join Date
    Dec 2007
    Location
    Bed Room
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL Join Help needed

    Hello! i am developing a small web application.. and there are 2 tables in my database

    1. tbl_categories(cat_id, cat_title)
    2. tbl_products(fldproduct_id,fldprotitle,product_description,cat_id,fldcredits)

    and i am using following code for fetching records from both tables..

    <table border="1">
    <tr>
    <td>Product Title</td>
    <td>Credits</td>
    <td>Category</td>
    </tr>
    <?
    $dbConn = mysql_connect("localhost","root","me");
    $db = mysql_select_db("db_gsmeasy",$dbConn);
    $sql = "SELECT tbl_products.fldprotitle, tbl_products.product_description, tbl_products.fldcredits,tbl_categories.cat_title FROM tbl_categories,tbl_products WHERE tbl_products.cat_id=tbl_categories.cat_id";

    $rs = mysql_query($sql);

    while($row = mysql_fetch_array($rs)) {
    printf("<tr><td>%s</td><td>%s</td><td>%d</td></tr>\n",$row['fldprotitle'],$row['fldcredits'],$row['cat_title']);
    }
    ?>
    </table>
    i want output as following table.

    Product Title | Credits | Category
    abc | 1000 | title of category(e.g box)


    but when i executed the above mentioned code .. it fetches first two fields quite right but id displays 0 at 3rd field this my required table..

    could anybody help me.....

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perhaps because you're using the &#37;d token but cat_title is a string. Cast an arbitrary string to a number and you usually get 0.

  3. #3
    SitePoint Zealot impunjabians's Avatar
    Join Date
    Dec 2007
    Location
    Bed Room
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks fetched successfully.. there was a problem at format specifier.

  4. #4
    SitePoint Zealot impunjabians's Avatar
    Join Date
    Dec 2007
    Location
    Bed Room
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how LEFT or RIGHT JOINS can help in this code?

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    They would allow you to retrieve all rows from one of the tables even if there was no matching row in the other.


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
  •