Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109217 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 88057 invoked from network); 23 Mar 2020 01:09:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Mar 2020 01:09:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 243F21804B8 for ; Sun, 22 Mar 2020 16:33:40 -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,RCVD_IN_DNSWL_NONE, 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-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 ; Sun, 22 Mar 2020 16:33:39 -0700 (PDT) Received: by mail-wm1-f52.google.com with SMTP id r7so11957749wmg.0 for ; Sun, 22 Mar 2020 16:33:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=craigfrancis.co.uk; s=default; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=T3q1k+xPepD6F72rgnopXe7cqyFNZPW07IfxQy/Y+Lc=; b=ANP1KMhAmo5365w8qZ4O5O+e08Sd3L0RjYtqz1zsdMuZRkNzN0ApI/MCrHWz/FClRw HCOvjZACeU+lUYkgEcImEpsm6OMOv/BW8zAHTIsLvilRT4O3YGbMb0/aN7JjbuzsNNuU W5hytHUedfbblRiXPPWgEDHDxrSXcM0OUY7K4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=T3q1k+xPepD6F72rgnopXe7cqyFNZPW07IfxQy/Y+Lc=; b=YH0gxMQcJ9aVgWW9d1DYzlRo6g34V4vgCzicSxO606eiuhVrXqU/BKAZFpCYabKl5C wf842V//I1CXNJJMdokpykRJ1199Owe+7PzaeRV0cA2NFqMm3vnDgJ5U5DSVBsTB7t94 FUE1lCNuovB8trDIPDDiSk7O3qsfhUFfaToolVU3veWD0dJ86+oLbIPVM1CAVEbjbEjo uEc19SBl4px8UPzXmUFbCb8OgdpROks3BzTbGfk06+2hYcl7Z3nDTRfcUiU0YgWGdBm2 z/67Sy9LyUwkqeZN7BaKOe2j831KW6o06kOt8gr4g6IDaFl/y4YaOflAK2R0jRcHgqoI 2gUA== X-Gm-Message-State: ANhLgQ1LBJDZG1nMGxwonlTvTs47iqslDdjzFX21xEaM+P1558UY8IWX ji9FBkAoqf6/swBfExjlN+47nA== X-Google-Smtp-Source: ADFU+vskT2O7WjUjykGmNurRRcNcNPbRPS6hVQd5r8vlyXcUcIMzNpG5j4Z3LU4gCJ3y33aLNRItjg== X-Received: by 2002:a1c:3d5:: with SMTP id 204mr4689985wmd.188.1584920016253; Sun, 22 Mar 2020 16:33:36 -0700 (PDT) Received: from [192.168.1.10] ([92.237.247.170]) by smtp.gmail.com with ESMTPSA id s15sm15364840wrm.15.2020.03.22.16.33.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Mar 2020 16:33:35 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) In-Reply-To: Date: Sun, 22 Mar 2020 23:33:34 +0000 Cc: PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: <10B08096-5A36-4F69-B60F-3CCC8AF7F66D@craigfrancis.co.uk> References: <3CE6E742-33E9-4686-93C4-2B1DB1E405B7@craigfrancis.co.uk> To: Levi Morrison X-Mailer: Apple Mail (2.3608.60.0.2.5) Subject: Re: [PHP-DEV] Making mysqli easier to use with parameters From: craig@craigfrancis.co.uk (Craig Francis) 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 =3D mysqli_fetch_assoc($statement->result)) { } Craig > On 22 Mar 2020, at 15:28, Levi Morrison = wrote: >=20 >> >=20 >> $in_sql =3D implode(',', array_fill(0, count($ids), '?')); >>=20 >> $sql =3D 'SELECT id, name FROM user WHERE id IN (' . $in_sql . = ')'; >>=20 >> if ($statement =3D $db->prepare($sql)) { >>=20 >> $params =3D [str_repeat('i', count($ids))]; >> foreach ($ids as $key =3D> $value) { >> $params[] =3D &$ids[$key]; // Must be a reference, not = ideal. >> } >> call_user_func_array(array($statement, 'bind_param'), = $params); >>=20 >> $statement->execute(); >>=20 >> $result =3D $statement->get_result(); >>=20 >> while ($row =3D mysqli_fetch_assoc($result)) { >> print_r($row); >> } >>=20 >> } >>=20 >> ?> >=20 > Written in my email client without error handling code, so apologies > if it's not quite correct: >=20 > $in =3D join(',', array_fill(0, count($ids), '?')); > $select =3D "SELECT id, name FROM user WHERE id IN ({$in});"; > $statement =3D $mysqli->prepare($select); > $statement->bind_param(str_repeat('i', count($ids)), ...$ids); > $statement->execute(); >=20 > This is part of my [highest score answer on StackOverflow][1]. > Critically, applying the ids array as `...` will handle the > by-reference passing. >=20 > 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. >=20 > [1]: https://stackoverflow.com/a/23641033/538216