Question re getting a single value from a mysql database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Spidah

    Question re getting a single value from a mysql database

    If I run a mysql query that gets a single field from a row in a database
    table, what is the easiest way to get the value from the result?

    I normally do something like this:

    $query = "SELECT name FROM users WHERE id = 1"
    $result = mysql_query($qu ery);
    list($name) = mysql_fetch_row ($result);

    Obviously this is one less line to write than

    $row = mysql_fetch_row ($result);
    $name = $row['name'];

    Is there a simpler way than using the list function with a single
    parameter?

    Thanks

    Hamilton


  • Jan Pieter Kunst

    #2
    Re: Question re getting a single value from a mysql database

    In article <fkFdc.10327$d% 6.183285@news.x tra.co.nz>,
    "Spidah" <h.laughland@eg gstra.co.nz> wrote:
    [color=blue]
    > If I run a mysql query that gets a single field from a row in a database
    > table, what is the easiest way to get the value from the result?
    >
    > I normally do something like this:
    >
    > $query = "SELECT name FROM users WHERE id = 1"
    > $result = mysql_query($qu ery);
    > list($name) = mysql_fetch_row ($result);
    >
    > Obviously this is one less line to write than
    >
    > $row = mysql_fetch_row ($result);
    > $name = $row['name'];
    >
    > Is there a simpler way than using the list function with a single
    > parameter?
    >
    > Thanks
    >
    > Hamilton[/color]

    One line less is of course this:

    $result = mysql_query("SE LECT name FROM users WHERE id = 1");
    list($name) = mysql_fetch_row ($result);

    Or even like this (though it borders on unreadable, I wouldn't recommend
    it):

    list($name) = mysql_fetch_row (mysql_query("S ELECT name FROM users WHERE
    id = 1"));

    Personally, I prefer to use PEAR::DB, where you can do this:

    $name = $database_objec t->getOne("SELE CT name FROM users WHERE id = 1");


    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • MJaC

      #3
      Response: getting a single value from a mysql database

      Why not just get it like this, I also changed the SQL a bit.

      $username = mysql_result(my sql_query("SELE CT `name` FROM `users` WHERE
      `id`='1' "), 0, 'name');

      Comment

      • Andy Hassall

        #4
        Re: Response: getting a single value from a mysql database

        On Sun, 18 Apr 2004 19:39:33 +0100, MJaC <mjac@mjac.co.u k> wrote:
        [color=blue]
        >Why not just get it like this, I also changed the SQL a bit.
        >
        >$username = mysql_result(my sql_query("SELE CT `name` FROM `users` WHERE
        >`id`='1' "), 0, 'name');[/color]

        Because that doesn't give you a way of checking for an error coming from the
        mysql_query call.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        • MJaC

          #5
          Re: Response: getting a single value from a mysql database

          > Andy Hassall wrote something silly:[color=blue]
          > Because that doesn't give you a way of checking for an error coming from the
          > mysql_query call.[/color]

          Come on, why would there be an error with the query SELECT `name` FROM
          `users` WHERE `id`='1'
          Checking queries for errors also brings exploits to light, heard of SQL
          Injections?

          I see no problem, however IF you want to be picky, and you are likely to
          get errors:

          $username = mysql_query("SE LECT `name` FROM `users` WHERE `id`='1' ") or
          die("An unexpected error has occured!<br />" . mysql_error() . ");
          $username = mysql_result($u sername, 0, 'name');

          Comment

          • Andy Hassall

            #6
            Re: Response: getting a single value from a mysql database

            On Sun, 18 Apr 2004 22:14:54 +0100, MJaC <mjac@mjac.co.u k> wrote:
            [color=blue][color=green]
            >> Andy Hassall wrote something silly:
            >> Because that doesn't give you a way of checking for an error coming from the
            >> mysql_query call.[/color]
            >
            >Come on, why would there be an error with the query SELECT `name` FROM
            >`users` WHERE `id`='1'[/color]

            Syntax errors. Wrongly named identifiers. Permissions. Out of memory or disk
            space. Corruption. Bugs. Disconnection from database server. Other unexpected
            circumstances.
            [color=blue]
            >Checking queries for errors also brings exploits to light, heard of SQL
            >Injections?[/color]

            How does checking for errors result in exploits? That's absurd. Proceeding
            past errors without checking results in, at best, unexpected behaviour as you
            start passing FALSE to things that expect result set resources.
            [color=blue]
            >I see no problem, however IF you want to be picky, and you are likely to
            >get errors:
            >
            >$username = mysql_query("SE LECT `name` FROM `users` WHERE `id`='1' ") or
            >die("An unexpected error has occured!<br />" . mysql_error() . ");
            >$username = mysql_result($u sername, 0, 'name');[/color]

            That is exactly what I meant. Check that mysql_query returns a valid result
            set resource before attempting to use it in mysql_result.

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

            Comment

            Working...