Sujet : Re: VMS x86-64 database server
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.os.vmsDate : 11. Jul 2025, 01:24:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <104pljh$14565$5@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Pan/0.162 (Pokrosvk)
On Thu, 10 Jul 2025 19:57:32 -0400, Arne Vajhøj wrote:
(and yes - everybody should use PDO instead of mysqli!)
>
$con = new PDO('mysql:host=arnepc5;dbname=test', 'arne', 'hemmeligt');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$stmt = $con->prepare('SELECT the data needed');
if(!$stmt) {
echo $con->errorInfo()[2] . "\r\n";
goto finish;
}
if(!$stmt->execute()) {
echo $stmt->errorInfo()[2] . "\r\n";
goto finish;
}
That is so clunky without exceptions. You’d think normal programmers
would get fed up of continually writing prepare/execute/fetch
sequences. But PHP programmers don’t seem to think like normal people.
Here’s a utility function I wrote for my Python code years ago, and
use every time I want to retrieve data from an SQL database. This is
the SQLite version using the APSW wrapper:
def db_iter(conn, cmd, values = None, mapfn = lambda x : x) :
"executes cmd on a new cursor from connection conn and yields" \
" the results in turn."
for item in conn.cursor().execute(cmd, values) :
yield mapfn(item)
#end for
#end db_iter
So getting and processing records is as simple as
for entry in db_iter(conn, "select ..." ...) :
... do something with entry ...
#end for