mySQL query output to page

Discussion in 'PHP' started by Joobz, Jan 15, 2008.

  1. #1
    If someone has a simple script snippet that runs a query and outputs it to a web page, please post it. I got the mySQL query part right, I just can't figure out why PHP won't output it to the browser.

    Or, if you have a link. Most of the examples I am finding online have a variable being inserted into the query via a form with the POST method.

    Much appreciated!
     
    Joobz, Jan 15, 2008 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Can you post what you have done so far with your script ?

    Brew
     
    Brewster, Jan 15, 2008 IP
  3. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $query = mysql_query("SELECT * from TABLE") or die(mysql_error());
    if(mysql_num_rows($query) > 0)
    {
        while($a=mysql_fetch_array($query))
        {
            echo $a['ID'];
            echo $a['name'];
        }
    }
    else
    {
       echo 'No Results Found';
    }
    Code (markup):
     
    lfhost, Jan 15, 2008 IP
  4. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There is no input from a form, just a standard query and display:
    
    <?php
    $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30";
    $result = mysql_query($query);
    while($arr = mysql_fetch_assoc($result)){
       extract($arr);
       echo "$result ";
    }
    ?>
    
    PHP:
    I'm a noob but I thought I might have had it....
     
    Joobz, Jan 15, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Try this:

    
    <?php
    $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30";
    $result = mysql_query($query);
    while($arr = mysql_fetch_assoc($result)){
       echo '<pre>';
       print_r($result);
       echo '</pre>';
       $outputted = true;
    }
    if (!$outputted){
       echo " - No Results Found - ";
    }
    ?>
    
    PHP:
     
    jayshah, Jan 15, 2008 IP
  6. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    All I ever get is an output list of the same value. "Resource id #5"
     
    Joobz, Jan 15, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Try that, adjusted the line. Sorry!

    
    <?php
    $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30";
    $result = mysql_query($query);
    while($arr = mysql_fetch_assoc($result)){
       echo '<pre>';
       print_r($arr); // Adjusted line
       echo '</pre>';
       $outputted = true;
    }
    if (!$outputted){
       echo " - No Results Found - ";
    }
    ?>
    
    Code (markup):
     
    jayshah, Jan 15, 2008 IP
  8. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8

    Yeah, I tried that too but go this as an output to the browser:

    
    Array
    (
        [id] => 18
        [search_keyword] => podcast
        [search_time] => 2008-01-15 12:01:22
    )
    
    Array
    (
        [id] => 17
        [search_keyword] => ipod
        [search_time] => 2008-01-15 11:54:10
    )
    
    Array
    (
        [id] => 16
        [search_keyword] => dvd
        [search_time] => 2008-01-15 10:08:16
    )
    
    Array
    (
        [id] => 15
        [search_keyword] => phone
        [search_time] => 2008-01-15 09:59:57
    )
    
    Array
    (
        [id] => 14
        [search_keyword] => mysql
        [search_time] => 2008-01-14 21:58:00
    )
    
    
    Code (markup):
    I can't figure this one out honestly.
     
    Joobz, Jan 15, 2008 IP
  9. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for your assistance everyone... I figured it out, it was in my query (which I originally thought was correct)

    
    <?php
    $query = "SELECT search_keyword FROM search_terms ORDER BY search_time DESC LIMIT 6";
    $result = mysql_query($query);
    while($arr = mysql_fetch_assoc($result)){
       extract($arr);
       echo $arr['search_keyword'] ;
    }
    ?>
    
    PHP:
     
    Joobz, Jan 15, 2008 IP
  10. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #10
    I wasn't sure what you wanted from the output, so I printed the entire array so you can see everything in that row. Sorry ;)

    Jay
     
    jayshah, Jan 15, 2008 IP
  11. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #11
    it's cool, thanks so much for the assistance!
     
    Joobz, Jan 15, 2008 IP