note 126799 added to mysqli-stmt.bind-result

From: Date: Tue, 25 Jan 2022 00:16:43 +0000
Subject: note 126799 added to mysqli-stmt.bind-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-217543@lists.php.net to get a copy of this message
From PHP 7.4+, you can use the spread operator (...) to easily automate the process of assigning variables to the query statement, and to fetch data when executing a SELECT request using prepared statements. However, the spread operator takes in consideration the size of the array. So it must be pre allocated before used in any context. <?php $link = // Create DB Connection $query_text = "SELECT mission, year, report FROM table WHERE id=? AND name=?;"; // Data to assign $types = "is"; $param = [0, "John Titor"]; // Prepare and Execute Statement $stmt = mysqli_stmt_prepare($link, $query_text); if (mysqli_stmt_execute($stmt)) { // We know we are going to retrieve 3 columns: Mission, Year and Report. $output = [0, 0, 0]; // Behind the scenes, the spread operator gives this idea when used: // mysqli_stmt_bind_result($link, $output[0], $output[1], $output[2]); mysqli_stmt_bind_result($stmt, ...$output); // Fetch data while (mysqli_stmt_fetch($stmt)) { echo "Mission: " . $output[0] . "\n"; echo "Year: " . $output[1] ."\n"; echo "Report: " . $output[2] . "\n"; } } else { echo "Error: Wrong Timeline\n"; } // Close DB Connection ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 200.53.25.51) ---- Manual Page -- https://php.net/manual/en/mysqli-stmt.bind-result.php Edit -- https://main.php.net/note/edit/126799 Del: integrated -- https://main.php.net/note/delete/126799/integrated Del: useless -- https://main.php.net/note/delete/126799/useless Del: bad code -- https://main.php.net/note/delete/126799/bad+code Del: spam -- https://main.php.net/note/delete/126799/spam Del: non-english -- https://main.php.net/note/delete/126799/non-english Del: in docs -- https://main.php.net/note/delete/126799/in+docs Del: other reasons-- https://main.php.net/note/delete/126799 Reject -- https://main.php.net/note/reject/126799 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217543) next »