Jump to content

Put SELECT values into Array?


doubledee

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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	
?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.