Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Location
    /etc/passwd
    Posts
    19,110
    Tokens
    1,139

    Latest Awards:

    Default Total PHP/MySQL noob, how do I grab a row and then output it?

    Hey,

    I'm a total n00b with PHP/MySQL and I'm learning so;

    How do I grab the data from a MySQL table? I have a table called siteconfig and in there I want to grab the single result from sitename.

    Now, I did:
    PHP Code:
    $sitenquery "SELECT sitename FROM siteconfig";
    $sitendb mysql_query($sitenquery);
    $sitename mysql_result($sitendb); 
    and then:
    PHP Code:
    <title><?php echo($sitename); ?></title>
    But it doesn't work?

    Sorry if this is a stupid question but I am still learning!
    Quote Originally Posted by Chippiewill View Post
    e-rebel forum moderator
    :8

  2. #2
    Join Date
    Nov 2008
    Location
    Cambridge, UK
    Posts
    901
    Tokens
    100

    Default

    If you wanted a single row and you knew, for example, that the ID was, again for example, 1, you could do this:

    Code:
    $query = mysql_query("SELECT * FROM `siteconfig` WHERE `id` = '1'");
    $row = mysql_fetch_assoc($query);
    
    echo $row['sitename'];
    That should work
    we're smiling but we're close to tears, even after all these years

  3. #3
    Join Date
    Apr 2010
    Location
    Newcastle
    Posts
    655
    Tokens
    50

    Default

    If you just want one field, use mysql_result like you tried.

    PHP Code:
    <?php
    $query 
    mysql_query("SELECT `sitename` FROM `siteconfig`;");
    $sitename mysql_result($query,0); // From the 0th row

    echo $sitename;
    ?>
    If you want the whole result set in an array, use MattFr's method.
    Last edited by Apolva; 16-05-2010 at 03:22 PM.

Posting Permissions

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