Page 1 of 2

database abstraction functions...

Posted: Mon Jul 16, 2007 4:45 am
by verlaine101
Firstly a quick hello since this is my first post on here.

I'm trying to undestand classes by building a databse abstraction layer, however I have hit a brick wall with this function.

What I'm trying to do is write a function that will return all records in a table and output them to pre-formatted string.

This is the function

Code: Select all

function rowFormated($table,$output)
        {
        $query=mysql_query("SELECT * FROM $table");
        while ($row =@mysql_fetch_array($query))
                {extract($row);
                $result .= $output;};
return $result;
}


I see the function being executed like this

Code: Select all

$contacts=rowFormatted(table,"name:$name Address: $address<br/>");
echo $contacts;
the problem I have is the vars sent in the formatting string are not be replaced with the values from the database.

Posted: Mon Jul 16, 2007 5:46 am
by volka
You might not want to hear/read this but I strongly suggest using an existing library before writing one.
verlaine101 wrote:$contacts=rowFormatted(table,"name:$name Address: $address<br/>");
the variable substitution takes place there, exactly there. if you try

Code: Select all

function rowFormated($table,$output) 
echo 'Debug: ', $output, "<br />\n";
you will probably get something like name: Address: <br/> because the (non-existing) variables have already been substituted.

Variable substitution doesn't take place in single quoted strings
With $contacts=rowFormatted(table,'name:$name Address: $address<br/>');
$output will contain the $variables
But you have to tell php that it should (re-)evaluate the string, see http://de2.php.net/eval
And now that I've mentioned eval() we can bet on how long it takes until someone links to a "eval is evil"-article ;)

Posted: Mon Jul 16, 2007 6:51 am
by verlaine101
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks for your reply I have the modified code working now.

Code: Select all

<?
function rowFormated($table,$output)
	{
	$query=mysql_query("SELECT * FROM $table");
	while ($row =@mysql_fetch_array($query))
		{extract($row);
		$functoutput=$output;
		eval("\$functoutput = \"$functoutput\";");
		$result .=$functoutput;
};
return $result;
}
include "connect_db.php";
$contacts=rowFormatted(table,'name:$name Address: $address<br/>');
echo $contacts;
?>
In my first attempt the $output var was be modified on the first iteration hence the $functoutput var which is re-written on each loop. I can't say I fully understand the eval function but it seems to work.

BTW I'm not seriously trying to write a database abstraction layer, just using it as a test case to learn more about php functions and classes.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 16, 2007 6:54 am
by feyd
Stay away from the eval() function (until you are very familiar with the language.)

Posted: Mon Jul 16, 2007 6:57 am
by verlaine101
Can you recommend an alternative to it?

Posted: Mon Jul 16, 2007 7:01 am
by feyd
What is in $output?

Posted: Mon Jul 16, 2007 7:04 am
by verlaine101
$output is like a template for the line of html containing vars from the database I wish to output. In the example I've given the database vars are $name and $address.


Code: Select all

$contacts=rowFormatted(table,'name:$name Address: $address<br/>');

Posted: Mon Jul 16, 2007 7:07 am
by Begby
Take a look at sprintf()

Posted: Mon Jul 16, 2007 7:13 am
by verlaine101
Is the eval method safe if not accepting user input?

Posted: Mon Jul 16, 2007 7:16 am
by Begby
verlaine101 wrote:Is the eval method safe if not accepting user input?
I have never ran into a problem where I absolutely had to use eval. Best practice is to never use it at all.

Posted: Mon Jul 16, 2007 7:21 am
by volka
Btw: I might be a bit picky but...
Where's the abstraction layer and where are the classes? ;)

Posted: Mon Jul 16, 2007 7:24 am
by verlaine101
Had a look at

Code: Select all

sprintf()
but I'm not sure how it could handled strings that have an undetermined format.

Posted: Mon Jul 16, 2007 7:25 am
by Begby
What do you mean undetermined format?

Posted: Mon Jul 16, 2007 7:27 am
by Begby
volka wrote:Btw: I might be a bit picky but...
Where's the abstraction layer and where are the classes? ;)
http://www.google.com/search?hl=en&q=ph ... gle+Search

Posted: Mon Jul 16, 2007 7:27 am
by verlaine101
volka wrote:Btw: I might be a bit picky but...
Where's the abstraction layer and where are the classes? ;)
I was just looking for some advice on that particular function that will be part of my class. Probably not the best thread subject header I suppose. :)