[plug] Database advice - Newbie

Ryan ryan at is.as.geeky.as
Tue Jun 24 13:00:29 WST 2003


On Tue, 2003-06-24 at 12:47, James Devenish wrote:
> In message <1056427179.18164.10.camel at latte.internal.itmaze.com.au>
> on Tue, Jun 24, 2003 at 11:59:39AM +0800, Onno Benschop wrote:
> > I especially don't like constructing SQL text queries with PHP (is
> > there another way?).
> 
> > I' not sure what you're asking here,
> 
> Specifically, how to construct SQL queries (esp. UPDATEs) without having
> code that is like "UPDATE blah WHERE ".stuff." blah $stuff"

I don't use PHP, but surely it has some implementation of bind variables
on queries?  Is there a DBI module for PHP?

In Perl:
	
	use DBI;

	my $dbh = DBI->connect('DBI:mysql:somedb','user','pass') 
		or die "Couldn't connect to database: " . DBI->errstr;

        my $query_edit_asset = $dbh->prepare(qq{
            UPDATE assets
            SET description = ?, location = ?, category = ?
            WHERE id = ?
        });

	$query_edit_asset->execute($blah1, $blah2, $blah4, $blah5);

SELECTs are identical, with obvious row retrieval garb following.

BTW: you can also do the above as per your PHP example and
construct-a-query, but it is considered wildly inefficient under a large
array of circumstances.  It is talked about in the DBI documentation.

Ryan



More information about the plug mailing list