Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109248 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16221 invoked from network); 23 Mar 2020 21:05:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Mar 2020 21:05:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E364C180546 for ; Mon, 23 Mar 2020 12:29:51 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 23 Mar 2020 12:29:51 -0700 (PDT) Received: by mail-wr1-f42.google.com with SMTP id b2so18554542wrj.10 for ; Mon, 23 Mar 2020 12:29:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=craigfrancis.co.uk; s=default; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=5+JUlwa6/d6tQ27FRot1D747JgMpof/3OdOuQ3sQc+U=; b=QEUsNio82rdqMwL3Tlqc/b9vqRoUH85B9zJXyvzWnobIS9XVRZbj3VvmLt91jJKdGL cvPL3zIts9M2b+imwXPfMnP1W4/YJJwo3/dkv0qaG4W8lazj3NFeYTqDAWvI5GvxxGQh hOJs5ZU4yu6fpDJQXLsdRJA+jMyGGbPhc1p2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=5+JUlwa6/d6tQ27FRot1D747JgMpof/3OdOuQ3sQc+U=; b=mPW7P82Kuz7soV6T7ZSfRv5urd1cvhyPIlyAFdoSO9dMv3FnHG2iUa4aFuMj9GQqrY RFLjfNqG1wBRqLQfEng3d37b3ElYrELnvSiyJ91ZMNkJywFu6zYxFNTMFoo9D7/mAxAG 5LWMJ2UhGvtsW4u4th+ne7qTLTdLIM4WtU302Pd+53SNTrDLMb39B980dQ3+QrQNJfok 0LN9EsNIDUZCU3jMsijFOfvatWro+y+nzkAOTqqRupl4H/IiYsObVF/iOkEwT1rt+r+J 6ufWDPH32ESbl87mYiJBmtiG6+CS3ok0at4Nlib6xtKoZ16RuRKksPrwO+/Vc2Oe8mev 4rBw== X-Gm-Message-State: ANhLgQ0yBCiHvypJqOsIu8Cd1ewWpp2uHKMPEvo0aTdeMt66YEstIor6 cgPNoENYXFoe7iIPIEpDC/sNAlrSuxLyPyiEg0xaeDFbBTPYQEmW X-Google-Smtp-Source: ADFU+vvGAO7ehJV+/MsebrTFazEOvySXAmKAjHxcd2Azoh7UxXWJaSSmilBqXKJWwzg4iXiSmdmdZ0uNbjXlhidBjCs= X-Received: by 2002:adf:dc92:: with SMTP id r18mr25835698wrj.76.1584991787433; Mon, 23 Mar 2020 12:29:47 -0700 (PDT) MIME-Version: 1.0 References: <3CE6E742-33E9-4686-93C4-2B1DB1E405B7@craigfrancis.co.uk> <10B08096-5A36-4F69-B60F-3CCC8AF7F66D@craigfrancis.co.uk> In-Reply-To: Date: Mon, 23 Mar 2020 19:29:36 +0000 Message-ID: To: Joel Hutchinson Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000766f4205a18aa8c1" Subject: Re: [PHP-DEV] Making mysqli easier to use with parameters From: craig@craigfrancis.co.uk (Craig Francis) --000000000000766f4205a18aa8c1 Content-Type: text/plain; charset="UTF-8" Hi Joel, I assume you're under the handle Machavity in Room 11: https://chat.stackoverflow.com/transcript/message/48927577#48927577 https://gist.github.com/machavity/c84dad59bbc4d7d37b2d6e6bfd654df3 If not, I'd be up for your proposal as well, anything to make mysqli easier to use. Personally I'd still prefer being able to build up an array of parameters, as I'm creating the SQL string (same as PDO). But that's only because the parameter positions will change... which could be handled by $i++, or as the second link suggests, make the parameter argument optional (and presumably reset after execute, so the statement can be reused). Craig On Mon, 23 Mar 2020 at 12:19, Joel Hutchinson < Joel.Hutchinson@onlinecommercegroup.com> wrote: > Hey Craig, I think this might be in the same vein as something I'd love to > see done (PDO already does this). Proposed it a while back and have talked > to a few internals folks about it off and on > > https://externals.io/message/107857#107857 > > Either way, mysqli lacks any proper way to do dynamic binds on a single > basis. This seems like low hanging fruit to make mysqli easier to use > > -----Original Message----- > From: Craig Francis > Sent: Sunday, March 22, 2020 6:34 PM > To: Levi Morrison > Cc: PHP internals > Subject: Re: [PHP-DEV] Making mysqli easier to use with parameters > > On 22 Mar 2020, at 15:28, Levi Morrison > wrote: > > applying the ids array as `...` will handle the by-reference passing. > > > > That does help, thanks. > > But I still wonder if the ability to pass in a single array of parameters > to `$statement->execute()` would remove a step, and be a bit easier to use > for all queries. > > If this was for a search form, where it dynamically creates a SELECT with > a variety of different parameters, it gets tricky again. > > And there was the thing I tacked onto the end, where I would like to use a > `$statement->result` property to skip the use of `$statement->get_result()`: > > while ($row = mysqli_fetch_assoc($statement->result)) { > } > > Craig > > > > > > On 22 Mar 2020, at 15:28, Levi Morrison > wrote: > > > >> >> > >> $in_sql = implode(',', array_fill(0, count($ids), '?')); > >> > >> $sql = 'SELECT id, name FROM user WHERE id IN (' . $in_sql . ')'; > >> > >> if ($statement = $db->prepare($sql)) { > >> > >> $params = [str_repeat('i', count($ids))]; > >> foreach ($ids as $key => $value) { > >> $params[] = &$ids[$key]; // Must be a reference, not ideal. > >> } > >> call_user_func_array(array($statement, 'bind_param'), > >> $params); > >> > >> $statement->execute(); > >> > >> $result = $statement->get_result(); > >> > >> while ($row = mysqli_fetch_assoc($result)) { > >> print_r($row); > >> } > >> > >> } > >> > >> ?> > > > > Written in my email client without error handling code, so apologies > > if it's not quite correct: > > > > $in = join(',', array_fill(0, count($ids), '?')); > > $select = "SELECT id, name FROM user WHERE id IN ({$in});"; > > $statement = $mysqli->prepare($select); > > $statement->bind_param(str_repeat('i', count($ids)), ...$ids); > > $statement->execute(); > > > > This is part of my [highest score answer on StackOverflow][1]. > > Critically, applying the ids array as `...` will handle the > > by-reference passing. > > > > I don't think we need to improve the ergonmics of mysqli for this case > > specifically, as it's always going to require some dynamic SQL > > generation because of the variable number of parameters to bind. > > > > [1]: https://stackoverflow.com/a/23641033/538216 > > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, > visit: http://www.php.net/unsub.php > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000766f4205a18aa8c1--