Rob_On_LU_Forum Posted June 4, 2013 Share Posted June 4, 2013 I am attempting to use mysqli_multi_query on 5 SELECT stored routines. My goal is to use <option></option> values for 5 drop down product select lists on my site. Using the printf and the later print_r functions I can see the $row[2] $query results at the top of my list_rope_options6.html page, and the results are correct, in that mysqli_multi_query is indeed returning the correct number of rows from my tables. However I get the error: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given when the program loops through the results while command, which I have as while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) I remember Larry writing in PHP and MySQL For Dynamic Web Sites that he sometimes see beginning PHP developers muddle the process of fetching query results, and I am newbie playing in the mud, probably making this too hard by using the mysqli_multi_query! But before I move back to running the SELECT routines one at a time I thought I would post the code and this question. Will mysqli_fetch_array work with the mysqli_multi_query in order to use fetch_array on the <option> lists? I have tried other mysqli_fetch_array() Constants _ASSOC, _NUM and _BOTH but I continue to get the same error, string given. I get stuck because using the printf then print_r functions I can see the correct results, (all the rows the 5 SELECT routines should return, are being returned) but while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) is resulting in a string which I guess can not be used on the <option> select options as written. Cross fit speed jump ropes will be sold (eventually) and this is a hobby side project! Here is my testing php page named index6.php. At the very bottom is the first part of my_error_handler <?php require ('./config.inc.php'); require ('./mysql.inc.php'); $query = "CALL select_handle_length ('jumprope');"; $query .= "CALL select_rope_length ('jumprope');"; $query .= "CALL select_rope_gauge ('jumprope');"; $query .= "CALL select_handle_color ('jumprope');"; $query .= "CALL select_endcap_color ('jumprope');"; /* execute multi query */ if (mysqli_multi_query($dbc, $query)) { do { // store first result set if ($result = mysqli_use_result($dbc)) { while ($row = mysqli_fetch_row($result)) { printf("%s\n", $row[2]); } mysqli_free_result($result); } /* print divider */ if (mysqli_next_result($dbc)) { print_r("--\n"); } } while (mysqli_more_results($dbc)); include ('./list_rope_options6.html'); } mysqli_close($dbc); ?> -- ------------------ And here is the ./list_rope_options6.html page which is not styled, only one <option value> set at this point...----------------------- -- <?php // This page is included by index6.php. // This page displays the available jumprope options. // This page will make use of the query result $query. // The query returns an array of: option_name. // Only display the header once: $header = false; // Loop through the results: while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { // If the header hasn't been shown, create it: if (!$header) { ?> <form action="./index6.php" method="get"><input type="hidden" name="action" value="add" /><select name="Handle Length"> <?php // The header has now been shown: $header = true; } // End of $header IF. // Create each option: echo '<option value="' . $row['id'] . '">' . $row['Handle Length'] . '</option>'; } // End of WHILE loop. ?></select> <input type="submit" value="Add to Cart" class="button" /></p></form></div> </div> </div> </div> <div class="left-bot-corner"> <div class="right-bot-corner"> <div class="border-bot"></div> </div> </div> </div> <!-- box end --> -- ----------------- The Error Array ------------------- -- 6 inch 7 inch 8 inch -- -- 6 ft 6 ft 3 in 6 ft 6 in 6 ft 9 in 7 ft 7 ft 3 in 7 ft 6 in 7 ft 9 in 8 ft 8 ft 3 in 8 ft 6 in 8 ft 9 in 9 ft 9 ft 3 in 9 ft 6 in 9 ft 9 in 10 ft 10 ft 3 in 10 ft 6 in 10 ft 9 in 11 ft enter -- -- 3/32 in 3/64 in 1/16 in -- -- Maroon Black Pink Red Blue Yellow Green Purple Orange Teal/Light Blue Lime Green -- -- Brown Gray White Purple Yellow Orange Green Red Blue -- An error occurred in script '/Applications/MAMP/WORKING_SITES/productOptionTesting/list_rope_options6.html' on line 11:mysqli_fetch_array() expects parameter 1 to be mysqli_result, string givenArray( [0] => Array ( [function] => my_error_handler [args] => Array ( [0] => 2 [1] => mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given [2] => /Applications/MAMP/WORKING_SITES/productOptionTesting/list_rope_options6.html [3] => 11 [4] => Array ( [_GET] => Array ( ) [_POST] => Array ( ) [_COOKIE] => Array ( ) [_FILES] => Array ( ) [live] => [contact_email] => you@example.com [dbc] => mysqli Object ( [affected_rows] => 0 [client_info] => 5.5.29 [client_version] => 50529 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 0 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => 5.5.29 [server_version] => 50529 [stat] => Uptime: 7805 Threads: 1 Questions: 1686 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.216 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 106 [warning_count] => 0 ) [query] => CALL select_handle_length ('jumprope');CALL select_rope_length ('jumprope');CALL select_rope_gauge ('jumprope');CALL select_handle_color ('jumprope');CALL select_endcap_color ('jumprope'); [result] => mysqli_result Object Link to comment Share on other sites More sharing options...
Antonio Conte Posted June 5, 2013 Share Posted June 5, 2013 The error is that your param is $query when it should be $result. To be more specific, the mysqli_fetch_array function needs a result resource. That resource is created by the mysqli query functions. You pass it the query string now, not the needed result resource. Moving forward, If you ever get the same error with Boolean instead of string, then it means your query has errors. Now you are just passing the wrong variable. 2 Link to comment Share on other sites More sharing options...
Recommended Posts