Динамически связывать параметры mysqli_stmt, а затем связывать результат (PHP)

Я пытаюсь динамически связать параметры mysql_stmt и получить результат в ассоциативном массиве. Я нашел этот пост здесь в stackoverflow, где Amber отправил ответ со следующим кодом:

Оригинальное сообщение: Как создать правильный класс расширения mysqli с подготовленными операторами?

«Предполагая, что вы на самом деле хотите написать свою собственную версию (в отличие от использования одной из существующих библиотек, которую предложили другие ответы, – и это тоже хорошие варианты) …

Вот несколько функций, которые вы можете счесть полезными для изучения. Первый позволяет связать результаты запроса с ассоциативным массивом, а второй позволяет передать в два массива, один – упорядоченный массив ключей, а другой – ассоциативный массив данных для этих ключей и привязать эти данные к подготовленное заявление: "

function stmt_bind_assoc (&$stmt, &$out) { $data = mysqli_stmt_result_metadata($stmt); $fields = array(); $out = array(); $fields[0] = $stmt; $count = 1; while($field = mysqli_fetch_field($data)) { $fields[$count] = &$out[$field->name]; $count++; } call_user_func_array(mysqli_stmt_bind_result, $fields); } function stmt_bind_params($stmt, $fields, $data) { // Dynamically build up the arguments for bind_param $paramstr = ''; $params = array(); foreach($fields as $key) { if(is_float($data[$key])) $paramstr .= 'd'; elseif(is_int($data[$key])) $paramstr .= 'i'; else $paramstr .= 's'; $params[] = $data[$key]; } array_unshift($params, $stmt, $paramstr); // and then call bind_param with the proper arguments call_user_func_array('mysqli_stmt_bind_param', $params); } 

Я попытался изучить код, чтобы понять, что он делает, и я сделал вторую функцию правильно, но я не знаю, что я должен сделать, чтобы использовать первую функцию. Как его использовать для извлечения массива, подобного mysqli_result :: fetch_assoc ()?

Я хочу, чтобы можно было использовать результат таким образом, как вы привыкли делать:

 while ($row = mysql_fetch_array($result)){ echo $row['foo']." ".$row['bar']; } 

Пожалуйста, помогите мне сделать некоторый прогресс в этом 🙂

Solutions Collecting From Web of "Динамически связывать параметры mysqli_stmt, а затем связывать результат (PHP)"

Хорошо, вот способ сделать это:

Отредактировано, чтобы исправить ошибку при извлечении нескольких строк

 $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); /* In my real app the below code is wrapped up in a class But this is just for example's sake. You could easily throw it in a function or class */ // This will loop through params, and generate types. eg 'ss' $types = ''; foreach($params as $param) { if(is_int($param)) { $types .= 'i'; //integer } elseif (is_float($param)) { $types .= 'd'; //double } elseif (is_string($param)) { $types .= 's'; //string } else { $types .= 'b'; //blob and unknown } } array_unshift($params, $types); // Start stmt $query = $this->connection->stmt_init(); // $this->connection is the mysqli connection instance if($query->prepare($sql)) { // Bind Params call_user_func_array(array($query,'bind_param'),$params); $query->execute(); // Get metadata for field names $meta = $query->result_metadata(); // initialise some empty arrays $fields = $results = array(); // This is the tricky bit dynamically creating an array of variables to use // to bind the results while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $fields[$var] = &$$var; } $fieldCount = count($fieldNames); // Bind Results call_user_func_array(array($query,'bind_result'),$fields); $i=0; while ($query->fetch()){ for($l=0;$l<$fieldCount;$l++) $results[$i][$fieldNames[$l]] = $fields[$fieldNames[$l]]; $i++; } $query->close(); // And now we have a beautiful // array of results, just like //fetch_assoc echo "<pre>"; print_r($results); echo "</pre>"; } 

Ответ от Emmanuel отлично работает, если выбран только один ряд! Если запрос выбирает несколько строк, $ result-Array для каждой строки возвращает результат, но результат всегда заполняется последней записью. С небольшим изменением в fetch () – пока он работает хорошо.

$ sqlStmt – это строка, заполненная mysql-query

$ params – это массив, заполненный переменными, которые должны пройти

$ results – пустой массив, содержащий результат

  if (!is_string($sqlStmt) || empty($sqlStmt)) { return false; } // initialise some empty arrays $fields = array(); $results = array(); if ($stmt = $this->prepare($sqlStmt)) { // bind params if they are set if (!empty($params)) { $types = ''; foreach($params as $param) { // set param type if (is_string($param)) { $types .= 's'; // strings } else if (is_int($param)) { $types .= 'i'; // integer } else if (is_float($param)) { $types .= 'd'; // double } else { $types .= 'b'; // default: blob and unknown types } } $bind_names[] = $types; for ($i=0; $i<count($params);$i++) { $bind_name = 'bind' . $i; $$bind_name = $params[$i]; $bind_names[] = &$$bind_name; } call_user_func_array(array($stmt,'bind_param'),$bind_names); } // execute query $stmt->execute(); // Get metadata for field names $meta = $stmt->result_metadata(); // This is the tricky bit dynamically creating an array of variables to use // to bind the results while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $fields[$var] = &$$var; } // Bind Results call_user_func_array(array($stmt,'bind_result'),$fields); // Fetch Results $i = 0; while ($stmt->fetch()) { $results[$i] = array(); foreach($fields as $k => $v) $results[$i][$k] = $v; $i++; } // close statement $stmt->close(); } 

Просто, чтобы сравнить отличные ответы от @Emmanuel и @matzino с кодом, который вы можете получить, если выбрать PDO над mysqli:

 $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); $stm = $query->prepare($sql); $stm->execute($params); $results = $stm->fetchAll(); // or fetch() or fetchColumn() depends on expected type - $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); $stm = $query->prepare($sql); $stm->execute($params); $results = $stm->fetchAll(); // or fetch() or fetchColumn() depends on expected type 

кричит, вот и все?

После использования вышеприведенного ответа я выяснил, что для меня была какая-то очистка, особенно часть «fieldNames []». Код ниже в процедурном стиле. Я надеюсь, что это пригодится кому-то.

Я вырезал код из класса, который я сделал, который может динамически запрашивать данные. Есть несколько вещей, которые я удалил, чтобы было легче читать. В классе я разрешаю пользователю определять таблицы определений и внешние ключи, чтобы данные ввода на интерфейсе были ограничены, а также параметры фильтрации и сортировки для указанных связанных данных. Это все параметры, которые я удалил, а также автоматический построитель запросов.

 $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); ////////////// GENERATE PARAMETER TYPES IF ANY ////////////// // This will loop through parameters, and generate types. ex: 'ss' $types = ''; $params_size = sizeof($params); if($params_size > 0) { foreach($params as $param) { if(is_int($param)) { $types .= 'i'; //integer }else if(is_float($param)) { $types .= 'd'; //double }else if(is_string($param)) { $types .= 's'; //string }else { $types .= 'b'; //blob and unknown } } array_unshift($params, $types); } //////////////////////////////////////////////////////////// // This is the tricky part to dynamically create an array of // variables to use to bind the results //below from http://php.net/manual/en/mysqli-result.fetch-field.php /* name The name of the column orgname Original column name if an alias was specified table The name of the table this field belongs to (if not calculated) orgtable Original table name if an alias was specified def Reserved for default value, currently always "" db Database (since PHP 5.3.6) catalog The catalog name, always "def" (since PHP 5.3.6) max_length The maximum width of the field for the result set. length The width of the field, as specified in the table definition. charsetnr The character set number for the field. flags An integer representing the bit-flags for the field. type The data type used for this field decimals The number of decimals used (for integer fields) */ /// FIELD TYPE REFERENCE /// /* numerics ------------- BIT: 16 TINYINT: 1 BOOL: 1 SMALLINT: 2 MEDIUMINT: 9 INTEGER: 3 BIGINT: 8 SERIAL: 8 FLOAT: 4 DOUBLE: 5 DECIMAL: 246 NUMERIC: 246 FIXED: 246 dates ------------ DATE: 10 DATETIME: 12 TIMESTAMP: 7 TIME: 11 YEAR: 13 strings & binary ------------ CHAR: 254 VARCHAR: 253 ENUM: 254 SET: 254 BINARY: 254 VARBINARY: 253 TINYBLOB: 252 BLOB: 252 MEDIUMBLOB: 252 TINYTEXT: 252 TEXT: 252 MEDIUMTEXT: 252 LONGTEXT: 252 */ if($stmt = mysqli_prepare($db_link, $query)) { // BIND PARAMETERS IF ANY // if($params_size > 0) { call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($params)); } mysqli_stmt_execute($stmt); $meta = mysqli_stmt_result_metadata($stmt); $field_names = array(); $field_length = array(); $field_type = array(); $output_data = array(); /// THIS GET THE NAMES OF THE FIELDS AND ASSIGNS NEW VARIABLES USING THE FIELD NAME. THESE VARIABLES ARE THEN SET TO NULL /// $count = 0; while($field = mysqli_fetch_field($meta)) { $field_names[$count] = $field->name;// field names $var = $field->name; $$var = null; $field_names_variables[$var] = &$$var;// fields variables using the field name $field_length[$var] = $field->length;// field length as defined in table $field_type[$var] = $field->type;// field data type as defined in table (numeric return) $count++; } setFieldLengthInfo($field_length); setFieldTypesInfo($field_type); $field_names_variables_size = sizeof($field_names_variables); call_user_func_array(array($stmt, 'bind_result'), $field_names_variables); $count = 0; while(mysqli_stmt_fetch($stmt)) { for($l = 0; $l < $field_names_variables_size; $l++) { $output_data[$count][$field_names[$l]] = $field_names_variables[$field_names[$l]];/// THIS SETS ALL OF THE FINAL DATA USING THE DYNAMICALLY CREATED VARIABLES ABOVE } $count++; } mysqli_stmt_close($stmt); echo "<pre>"; print_r($output_data); echo "</pre>"; } function makeValuesReferenced($arr) { $refs = array(); foreach($arr as $key => $value) $refs[$key] = &$arr[$key]; return $refs; } в $sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?"; $params = array('Australia','Victoria'); ////////////// GENERATE PARAMETER TYPES IF ANY ////////////// // This will loop through parameters, and generate types. ex: 'ss' $types = ''; $params_size = sizeof($params); if($params_size > 0) { foreach($params as $param) { if(is_int($param)) { $types .= 'i'; //integer }else if(is_float($param)) { $types .= 'd'; //double }else if(is_string($param)) { $types .= 's'; //string }else { $types .= 'b'; //blob and unknown } } array_unshift($params, $types); } //////////////////////////////////////////////////////////// // This is the tricky part to dynamically create an array of // variables to use to bind the results //below from http://php.net/manual/en/mysqli-result.fetch-field.php /* name The name of the column orgname Original column name if an alias was specified table The name of the table this field belongs to (if not calculated) orgtable Original table name if an alias was specified def Reserved for default value, currently always "" db Database (since PHP 5.3.6) catalog The catalog name, always "def" (since PHP 5.3.6) max_length The maximum width of the field for the result set. length The width of the field, as specified in the table definition. charsetnr The character set number for the field. flags An integer representing the bit-flags for the field. type The data type used for this field decimals The number of decimals used (for integer fields) */ /// FIELD TYPE REFERENCE /// /* numerics ------------- BIT: 16 TINYINT: 1 BOOL: 1 SMALLINT: 2 MEDIUMINT: 9 INTEGER: 3 BIGINT: 8 SERIAL: 8 FLOAT: 4 DOUBLE: 5 DECIMAL: 246 NUMERIC: 246 FIXED: 246 dates ------------ DATE: 10 DATETIME: 12 TIMESTAMP: 7 TIME: 11 YEAR: 13 strings & binary ------------ CHAR: 254 VARCHAR: 253 ENUM: 254 SET: 254 BINARY: 254 VARBINARY: 253 TINYBLOB: 252 BLOB: 252 MEDIUMBLOB: 252 TINYTEXT: 252 TEXT: 252 MEDIUMTEXT: 252 LONGTEXT: 252 */ if($stmt = mysqli_prepare($db_link, $query)) { // BIND PARAMETERS IF ANY // if($params_size > 0) { call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($params)); } mysqli_stmt_execute($stmt); $meta = mysqli_stmt_result_metadata($stmt); $field_names = array(); $field_length = array(); $field_type = array(); $output_data = array(); /// THIS GET THE NAMES OF THE FIELDS AND ASSIGNS NEW VARIABLES USING THE FIELD NAME. THESE VARIABLES ARE THEN SET TO NULL /// $count = 0; while($field = mysqli_fetch_field($meta)) { $field_names[$count] = $field->name;// field names $var = $field->name; $$var = null; $field_names_variables[$var] = &$$var;// fields variables using the field name $field_length[$var] = $field->length;// field length as defined in table $field_type[$var] = $field->type;// field data type as defined in table (numeric return) $count++; } setFieldLengthInfo($field_length); setFieldTypesInfo($field_type); $field_names_variables_size = sizeof($field_names_variables); call_user_func_array(array($stmt, 'bind_result'), $field_names_variables); $count = 0; while(mysqli_stmt_fetch($stmt)) { for($l = 0; $l < $field_names_variables_size; $l++) { $output_data[$count][$field_names[$l]] = $field_names_variables[$field_names[$l]];/// THIS SETS ALL OF THE FINAL DATA USING THE DYNAMICALLY CREATED VARIABLES ABOVE } $count++; } mysqli_stmt_close($stmt); echo "<pre>"; print_r($output_data); echo "</pre>"; } function makeValuesReferenced($arr) { $refs = array(); foreach($arr as $key => $value) $refs[$key] = &$arr[$key]; return $refs; }