Раз и навсегда! Используйте эту функцию для получения результатов по неограниченному количеству запросов в любом месте вашего скрипта.
blockquote>Функция:
Вы просто передаете результат мультипроцесса функции, и это возвращает все результаты и ошибки, обнаруженные в каждом запросе.
function loop_multi($result){ //use the global variable $conn in this function global $conn; //an array to store results and return at the end $returned = array("result"=>array(),"error"=>array()); //if first query doesn't return errors if ($result){ //store results of first query in the $returned array $returned["result"][0] = mysqli_store_result($conn); //set a variable to loop and assign following results to the $returned array properly $count = 0; // start doing and keep trying until the while condition below is not met do { //increase the loop count by one $count++; //go to the next result mysqli_next_result($conn); //get mysqli stored result for this query $result = mysqli_store_result($conn); //if this query in the loop doesn't return errors if($result){ //store results of this query in the $returned array $returned["result"][$count] = $result; //if this query in the loop returns errors }else{ //store errors of this query in the $returned array $returned["error"][$count] = mysqli_error($conn); } } // stop if this is false while (mysqli_more_results($conn)); }else{ //if first query returns errors $returned["error"][0] = mysqli_error($conn); } //return the $returned array return $returned; }
Использование:
$query = "INSERT INTO table1 (attribute1) VALUES ('value1');"; $query .= "INSERT INTO table2 (attribute2) VALUES ('value2');"; $query .= "SELECT * FROM table3;"; //execute query $result = mysqli_multi_query($conn, $query); //pass $result to the loop_multi function $output = loop_multi($result);
Выход
$ включает в себя 2 массива «результат» и «результат», ошибка ", упорядоченная по запросу. Например, если вам нужно проверить, произошли ли какие-либо ошибки при выполнении третьего запроса и получить его результат, вы можете сделать:
if(isset($output['error'][2]) && $output['error'][2] !== ""){ echo $output['error'][2]; }else{ while($row = $output['result'][2]->fetch_assoc()) { print_r($row); } }
Вы развертываете данные каждый раз в цикле, как <td>{i.price_a} test a..</td>
. здесь, если i.price_a не найден в объекте, что означает, что он не определен, поэтому его рендеринг как <td>{undefined} test a..</td>
.
undefined не будет отображаться в jsx, и вы увидите только <td>test a..</td>
.
Чтобы решить эту проблему, вы можете проверить каждый объект, если обнаружены данные только тогда, когда часть td будет отображаться в противном случае, она станет черной.
, т.е.
<td>{i.price_a ? `${i.price_a} test a..` : ''} </td>
ИЛИ
<td>{i.price_a ? i.price_a + 'test a..' : ''} </td>