What do you think of this way of doing DB in PHP?

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

    What do you think of this way of doing DB in PHP?

    Hi group.

    I have just implemented an idea in PHP that I first saw in the OpenACS web
    framework.

    Basically the idea is to use dynamic evaluation (eval) to introduce the
    result of a database call as variables in the calling code.

    So the following code:

    db_one_row ("SELECT name, number FROM people LIMIT 1");

    Introduces the variables name and number (from the SQL statement). Meaning
    that I afterwards can write:

    echo $name;
    echo $number;

    Instead of writing something like:

    $rows = mysql_query("SE LECT name, number FROM people LIMIT 1");
    $row = mysql_fetch_arr ay($rows);
    echo $row[0];
    echo $row[1];

    Likewise I can read multiple rows by having the next method of an iterator
    introducing variables in caller's scope:

    $i = db_multi_row("S ELECT email, name FROM email_addresses ");
    while ($i.next()) echo "$name : $email";

    What do you think of this approach to database access? Is it good or is it
    bad?

    My implementation is here:


    -- Christian


  • Alan Little

    #2
    Re: What do you think of this way of doing DB in PHP?

    Carved in mystic runes upon the very living rock, the last words of
    Christian Hvid of comp.lang.php make plain:
    [color=blue]
    > Basically the idea is to use dynamic evaluation (eval) to introduce
    > the result of a database call as variables in the calling code.
    >
    > So the following code:
    >
    > db_one_row ("SELECT name, number FROM people LIMIT 1");
    >
    > Introduces the variables name and number (from the SQL statement).
    > Meaning that I afterwards can write:
    >
    > echo $name;
    > echo $number;
    >
    > Instead of writing something like:
    >
    > $rows = mysql_query("SE LECT name, number FROM people LIMIT 1");
    > $row = mysql_fetch_arr ay($rows);
    > echo $row[0];
    > echo $row[1];[/color]

    Your eval() method is far more complicated than necessary. Just use
    MySQL_Fetch_Ass oc() and extract(). If you're going to embed it in a
    function, you'll have to loop over it to globalise the variables, but
    again, you can just use variable variables rather than constructing a
    command to eval.
    [color=blue]
    > Likewise I can read multiple rows by having the next method of an
    > iterator introducing variables in caller's scope:[/color]

    An interesting idea.

    --
    Alan Little
    Phorm PHP Form Processor

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: What do you think of this way of doing DB in PHP?

      "Christian Hvid" <se@mit-site-vredungmand.dk> wrote in message news:<419ce320$ 0$22719$d40e179 e@nntp04.dk.tel ia.net>...[color=blue]
      > Hi group.
      >
      > I have just implemented an idea in PHP that I first saw in the OpenACS web
      > framework.
      >
      > Basically the idea is to use dynamic evaluation (eval) to introduce the
      > result of a database call as variables in the calling code.
      >
      > So the following code:
      >
      > db_one_row ("SELECT name, number FROM people LIMIT 1");
      >
      > Introduces the variables name and number (from the SQL statement). Meaning
      > that I afterwards can write:
      >
      > echo $name;
      > echo $number;[/color]

      If you break the "de facto" standards, other 3rd programmer may not
      be able to follow the code which will cost the maintenance. My
      prefered way is mysql_fetch_ass oc() and $row['name'], etc.

      --
      <?php echo 'Just another PHP saint'; ?>
      Email: rrjanbiah-at-Y!com

      Comment

      • Jay Donnell

        #4
        Re: What do you think of this way of doing DB in PHP?

        What happens if there is already a variable called $name?
        [color=blue]
        > So the following code:
        >
        > db_one_row ("SELECT name, number FROM people LIMIT 1");
        >
        > Introduces the variables name and number (from the SQL statement). Meaning
        > that I afterwards can write:
        >
        > echo $name;
        > echo $number;
        >
        > Instead of writing something like:
        >
        > $rows = mysql_query("SE LECT name, number FROM people LIMIT 1");
        > $row = mysql_fetch_arr ay($rows);
        > echo $row[0];
        > echo $row[1];[/color]

        Comment

        Working...