doubledee Posted April 3, 2012 Share Posted April 3, 2012 I have a Prepared Statement that runs a SELECT statement and returns 2 records, and I would like to store the Field Value for each Record into an Array. Here is the code I usually use for queries that return just a single value... // ****************** // Populate Form. * // ****************** // Build query. $q2 = "SELECT response FROM bio_answer WHERE member_id=?"; // Prepare statement. $stmt2 = mysqli_prepare($dbc, $q2); // Bind variable to query. mysqli_stmt_bind_param($stmt2, 'i', $memberID); // Execute query. mysqli_stmt_execute($stmt2); // Store results. mysqli_stmt_store_result($stmt2); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt2)>0){ // Details Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt2, $response); // Fetch record. mysqli_stmt_fetch($stmt2); // Close prepared statement. mysqli_stmt_close($stmt2); }else{ // Details Not Found. $_SESSION['resultsCode'] = 'DETAILS_NOT_FOUND_2133'; // Close prepared statement. mysqli_stmt_close($stmt2); // Set Error Source. $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME']; // Redirect to Display Outcome. header("Location: " . BASE_URL . "/members/results.php"); // End script. exit(); }//End of POPULATE FORM Can someone help me out with the syntax so that I get an end result like this... $answerArray[0] = 'I want to be my own boss!!' $answerArray[1] = 'Don't waste your time trying to do your own Taxes!' Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/ Share on other sites More sharing options...
Muddy_Funster Posted April 3, 2012 Share Posted April 3, 2012 There are a couple of ways that are effectivly the same - you loop through the recordset that is returned and then add each value in turn into an array, either defining the array key with a counter, using array_push or letting it build a multidimensional array. All that really changes is the type of loop and how you choose to get the info into the array. I'm not versed in mysqli, but if you want an example in mysql or sqlsrv shout back. Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1333854 Share on other sites More sharing options...
TimeBomb Posted April 3, 2012 Share Posted April 3, 2012 The PHP manual has an example of code that does exactly what you want it to do. http://us2.php.net/manual/en/mysqli.prepare.php Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1333856 Share on other sites More sharing options...
doubledee Posted April 3, 2012 Author Share Posted April 3, 2012 The PHP manual has an example of code that does exactly what you want it to do. http://us2.php.net/manual/en/mysqli.prepare.php I looked at that and didn't see anything that would help. I don't know OOP, and I want to stick with using mysqli Prepared Statements like I posted in my code above. I know I need to loop, but am unsure of what code to use. If someone could help me figure this out that would be great, because the Prepared Statements above are about the only style/format that I know how to use... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1333868 Share on other sites More sharing options...
litebearer Posted April 3, 2012 Share Posted April 3, 2012 having never used prepared statement, how would you simply loop thru a result set displaying one particular field from each returned row? You answer may lie there. Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1333876 Share on other sites More sharing options...
TimeBomb Posted April 4, 2012 Share Posted April 4, 2012 The PHP manual has an example of code that does exactly what you want it to do. http://us2.php.net/manual/en/mysqli.prepare.php I looked at that and didn't see anything that would help. I don't know OOP, and I want to stick with using mysqli Prepared Statements like I posted in my code above. I know I need to loop, but am unsure of what code to use. If someone could help me figure this out that would be great, because the Prepared Statements above are about the only style/format that I know how to use... Debbie See mysqli_stmt_fetch. This should be exactly what you are looking for. There are both OO and procedural examples provided in most PHP manual examples. Look under the OO example to see the procedural example. Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1334210 Share on other sites More sharing options...
Drummin Posted April 4, 2012 Share Posted April 4, 2012 Not having worked with this style of scripting before I can only guess. Hey, you don't like my answers usually anyway, so why not. <?php $answerArray=array(); // ****************** // Populate Form. * // ****************** // Build query. $q2 = "SELECT response FROM bio_answer WHERE member_id=?" // Prepare statement. $stmt2 = mysqli_prepare($dbc, $q2); // Bind variable to query. mysqli_stmt_bind_param($stmt2, 'i', $memberID); // Execute query. mysqli_stmt_execute($stmt2); // Store results. mysqli_stmt_store_result($stmt2); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt2)>0){ // Details Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt2, $response); // Fetch record. while (mysqli_stmt_fetch($stmt2)) { $answerArray[]=$response; } // Close prepared statement. mysqli_stmt_close($stmt2); }else{ // Details Not Found. $_SESSION['resultsCode'] = 'DETAILS_NOT_FOUND_2133'; // Close prepared statement. mysqli_stmt_close($stmt2); // Set Error Source. $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME']; // Redirect to Display Outcome. header("Location: " . BASE_URL . "/members/results.php"); // End script. exit(); }//End of POPULATE FORM ?> Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1334216 Share on other sites More sharing options...
Drummin Posted April 4, 2012 Share Posted April 4, 2012 I just tested the script. After adding limit 2 and a semi-colon at the end of the query it worked as expected. Must have lost that semi-colon when I copied it and didn't notice it on my last post. $q2 = "SELECT response FROM bio_answer WHERE member_id=? limit 2"; Resulting in $answerArray[0] = 'I want to be my own boss!!' $answerArray[1] = 'Don't waste your time trying to do your own Taxes!' Hope you get it working. Quote Link to comment https://forums.phpfreaks.com/topic/260243-put-select-values-into-array/#findComment-1334228 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.