473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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("SELECT name, number FROM people LIMIT 1");
$row = mysql_fetch_array($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("SELECT 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:
http://vredungmand.dk/programming/ez...ess/index.html

-- Christian
Jul 17 '05 #1
3 1454
Carved in mystic runes upon the very living rock, the last words of
Christian Hvid of comp.lang.php make plain:
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("SELECT name, number FROM people LIMIT 1");
$row = mysql_fetch_array($rows);
echo $row[0];
echo $row[1];
Your eval() method is far more complicated than necessary. Just use
MySQL_Fetch_Assoc() 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.
Likewise I can read multiple rows by having the next method of an
iterator introducing variables in caller's scope:


An interesting idea.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #2
"Christian Hvid" <se@mit-site-vredungmand.dk> wrote in message news:<41***********************@nntp04.dk.telia.ne t>...
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;


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_assoc() and $row['name'], etc.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3
What happens if there is already a variable called $name?
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("SELECT name, number FROM people LIMIT 1");
$row = mysql_fetch_array($rows);
echo $row[0];
echo $row[1];

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
28
by: john_sips_tea | last post by:
Just tried Ruby over the past two days. I won't bore you with the reasons I didn't like it, however one thing really struck me about it that I think we (the Python community) can learn from. ...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.