SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 66
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Echoing PHP from Database (Widget)

    Hi,

    I am trying add a widget to certain blog post so that it display products. Just like an image would appear. The code works fine when I paste onto a standard page but when I try to echo it from a database it displays the second peice of PHP code as it was standard text.

    Any suggestions please?

    PHP Code:
    <?php echo $query_row['name']; ?>

    PHP Code:
    <?php

    $query 
    mysql_query("
        SELECT name, linkname, product_id, price, discount, image_link
            FROM productdbase p
            INNER JOIN furniture_groups f
                ON
                    f.id = p.id
            WHERE
                linkname LIKE '%coffee%'
        LIMIT 15"
    );

    while (
    $query_row mysql_fetch_assoc($query)) { 

    ?>

    <div class="productrangeborder">
    <div class="productsdetailsborder">
    <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" class='productlink' rel="nofollow" ><?php echo $query_row['name']; ?>
    </a>
    </div>

    <div class="productimageborder">
    <a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" rel="nofollow"><img src="<?php echo $query_row['image_link']; ?>" alt="<?php echo $query_row['name']; ?>" /></a>
    </div>

    <div class="priceborder">Price <?php echo $query_row['price']; ?><br /></div><div class="discountborder">Save <?php echo $query_row['discount']; ?>%<br />
    </div></div>

    <?php 
     
    }
    ?>
    Last edited by SpacePhoenix; Aug 24, 2013 at 15:03. Reason: placed php tags around php code

  2. #2
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    You need to add table labels p and f to your field names in the query, e.g. p.name, p.linkname etc. You'll need that in your WHERE statement as well.
    I would quote and bracket your results keys when echoing.
    PHP Code:
    <?php echo "{$query_row['product_id']}"?>

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    You need to add table labels p and f to your field names in the query, e.g. p.name, p.linkname etc. You'll need that in your WHERE statement as well.
    I would quote and bracket your results keys when echoing.
    PHP Code:
    <?php echo "{$query_row['product_id']}"?>
    I'm not sure what you mean by "You need to add table labels p and f to your field names in the query, e.g. p.name, p.linkname etc" I thought I was already doing that.

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Be aware that the mysql_* extension is deprecated as of the current version of PHP and will very likely be removed from the next version of PHP. You should be migrating over to either the mysqli_* extension or to PDO
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    What I was suggesting was to add table identifier with the fields you are calling. Adding an error check wouldn't hurt either to show why a query might fail. Again though you should consider switching. As I don't know what tables belong to each field I will offer this.
    PHP Code:
    $sql "
        SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
            FROM productdbase p
            INNER JOIN furniture_groups f
                ON
                    f.id = p.id
            WHERE
                p.linkname LIKE '%coffee%'
        LIMIT 15"
    ;
    $result mysql_query($sql) or die(mysql_error());
    while (
    $query_row mysql_fetch_assoc($result)) { 

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The code works on a standard page but I cant echo it from a database.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    What I was suggesting was to add table identifier with the fields you are calling. Adding an error check wouldn't hurt either to show why a query might fail. Again though you should consider switching. As I don't know what tables belong to each field I will offer this.
    PHP Code:
    $sql "
        SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
            FROM productdbase p
            INNER JOIN furniture_groups f
                ON
                    f.id = p.id
            WHERE
                p.linkname LIKE '%coffee%'
        LIMIT 15"
    ;
    $result mysql_query($sql) or die(mysql_error());
    while (
    $query_row mysql_fetch_assoc($result)) { 
    Hi,

    Im really confused as to how this code is to work. What is the different between my code and this code.

  8. #8
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    The code works on a standard page but I cant echo it from a database.
    What does that mean?

    Are you getting query results?
    Do you have database connection in place?
    IF your query works then you should see result array using this.
    PHP Code:
    <?php

    $query 
    mysql_query("
        SELECT name, linkname, product_id, price, discount, image_link
            FROM productdbase p
            INNER JOIN furniture_groups f
                ON
                    f.id = p.id
            WHERE
                linkname LIKE '%coffee%'
        LIMIT 15"
    );

    while (
    $query_row mysql_fetch_assoc($query)) { 
        echo 
    "<pre>";
        
    print_r($query_row);
        echo 
    "</pre>";
    }
    ?>
    IF you are not getting results, does using the query I posted make a difference?
    Does it show an error?

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The echoes this: "; print_r($query_row); echo ""; } ?>

    The code I have been using so far works when I paste onto a page. However I cant get it to work when I echo the code from a database.

    I am trying to echo product displays on my blog instead of just a standard .php page but when I echo the code from a database it does not work properly.

  10. #10
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Is php available on your site?
    Does the page have php extension, e.g. index.php?

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi it is a .php page.

    Is there a method of echoing PHP from a database?

  12. #12
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    And if you query single table do you get result?
    PHP Code:
    <?php
    $sql 
    "SELECT name, linkname, product_id
            FROM productdbase 
            WHERE
                linkname LIKE '%coffee%'
        LIMIT 15"
    ;
    $result mysql_query($sql) or die(mysql_error());
    while (
    $query_row mysql_fetch_assoc($result)) {
        echo 
    "<pre>";
        
    print_r($query_row);
        echo 
    "</pre>";
    }
    ?>
    It should print out an array.

    Knowing what fields belong to which table might help.

  13. #13
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It echoes: "; print_r($query_row); echo ""; } ?>

    It does echo the array when I paste it onto a standard page.

  14. #14
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    OK, let's try two tables and identify tables with AS and use this same identifiers with fields. Please edit accordingly.
    PHP Code:
    <?php
    $sql 
    "
        SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
            FROM productdbase as p
            INNER JOIN furniture_groups as f
                ON
                    f.id = p.id
            WHERE
                p.linkname LIKE '%coffee%'
        LIMIT 15"
    ;
    $result mysql_query($sql) or die(mysql_error());
    while (
    $query_row mysql_fetch_assoc($result)) {
        echo 
    "<pre>";
        
    print_r($query_row);
        echo 
    "</pre>";
    }
    ?>

  15. #15
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This echoes: "; print_r($query_row); echo ""; } ?>

  16. #16
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Are the identifiers correct in that `image_link` is from table `furniture_groups`?
    I assume `productdbase` is your primary table. Maybe try
    LEFT JOIN instead of INNER JOIN

  17. #17
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes they are.

    The problem appears to be echoing the code from a database.

    Is there some very simple I can try?

  18. #18
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    You are not getting results from the joined query for some reason. It's not about echo, it's about the query. Are you using the error line like in my example?
    PHP Code:
    $result mysql_query($sql) or die(mysql_error()); 
    Please show current test query.

  19. #19
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But what is really confusing me is that the code I have works when I paste it onto a standard page. It doesn't work when I echo it from my database.

  20. #20
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Can you show your current test page please?

    "The code I have works" doesn't mean a thing. Are you talking about output display? It's a bad practice to be making these queries during output anyway but I'm just trying to help you get things working like you intend.

    Can you make a little test page that has DB connection at the top and our query?
    PHP Code:
     <?php
    include("conn.php");
    $sql "
        SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
            FROM productdbase as p
            INNER JOIN furniture_groups as f
                ON
                    f.id = p.id
            WHERE
                p.linkname LIKE '%coffee%'
        LIMIT 15"
    ;
    $result mysql_query($sql) or die(mysql_error());
    while (
    $query_row mysql_fetch_assoc($result)) {
        echo 
    "<pre>";
        
    print_r($query_row);
        echo 
    "</pre>";
    }
    ?>

  21. #21
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The page display the output when I paste it onto a page. But when I echo it from a database it does do anything.

    When I echo code from a database does it have any affect on it?

  22. #22
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Do I need to place it inside some form of frame.

    When I echo an Amazon frame it displays fine.

    Code:
    <iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=vacuumsacouk-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B004RJ0Y8S" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>

  23. #23
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    We're talking about queries not display.

  24. #24
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why does the query work when I paste it on a page but not when I echo it from a database?

    Could it be some sort connection problem, although it doesn't create any errors to suggest that.

  25. #25
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    507
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Why does the query work when I paste it on a page but not when I echo it from a database?

    Could it be some sort connection problem, although it doesn't create any errors to suggest that.
    What does that mean? "Paste a query on a page".

    Can you please make a test page?


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
  •