‫We'll continue with our API creation and I installed Notepad Plus.
‫Plus it's better click on edit with Notepad Plus Plus and you can see it's more beautiful than the notepad
‫in the windows.
‫Here I need to retrieve data from the result set of the prepared statement.
‫So for that I'll start with Stmt.
‫This function bind underscore result method.
‫This line binds variables to the result set columns in the order you provide.
‫So I'll create the variables here at question option one and the other options.
‫The variables question option one, two, three and four and the correct option are used to store the
‫values retrieved from the result set.
‫When the prepared statement is executed, the values from the selected columns will be stored in these
‫variables.
‫So if I execute this statement, I need to get all these columns question and store it inside the question
‫variable.
‫Go to the option one column and get the value of the record and put it inside.
‫Option one, option two, option three four and the correct option.
‫So all of these columns, all of these records in these columns will be stored inside the variables
‫here.
‫Again guys here we use the variables to store the values retrieved from the result set.
‫When the prepared statement is executed, the values from the selected column will be stored in these
‫variables.
‫Then I'll create an empty array because this array will store the variables.
‫Question option one, two, three and four and the correct option.
‫So to create an array in PHP, I'll start with the name of the PHP and the dollar sign questions.
‫Underscore array equals to array method.
‫This is how we create an array.
‫Then we need to traverse through all the questions and store them into the questions array.
‫For that I'll use a while loop.
‫A while loop is used to fetch rows from the result set of the prepared statement stmt.
‫So I'll start with stmt and I'll use the method fetch for each row.
‫Fetch it and you empty array named temp is created again.
‫Guys, I'll create an empty array.
‫So the loop the while loop continues as long as there are rows in the result set to fetch.
‫For each iteration of the loop, a row from the result set is fetched and the loop's body is executed
‫inside the loop and you empty array temp is created to temporarily store the data from the current row.
‫This array will hold the values for each column in the result set for the current row.
‫Then inside this loop I'll start with temp question equals to question variable.
‫This line assigns the value of the question variable to the question key in the temp array.
‫So here is the question key which is from the temp array equals to question variable that we get from
‫the binder underscore result function.
‫And I'll continue with the same algorithm I start with temp.
‫Here I'll specify option one equals to option one.
‫This is the key.
‫Option one of the temp array equals to option one which is the variable we get from the bind result.
‫Again guys I'll continue with others.
‫So.
‫As you loop through the result set, the temp array is updated for each row with the values from the
‫corresponding columns.
‫After populating the temp array, you can store it or use it as needed, and you might store each temp
‫array in another array to create a collection of questions and their options.
‫This is a common technique for processing and displaying tabular data from a database query.
‫So for that I need to use array underscore push method.
‫And I'll link the questions array with the temp array underscore push method in.
‫In PHP is used to add one or more elements to the end of an array.
‫You are using array underscore push method to add the temp array to another array named questions underscore
‫array.
‫This is a common approach for collecting data from a loop, like the one you have when iterating through
‫a database result set and storing it in an array.
‫So I am adding the temp array at the end of the questions array for each iteration of the while loop.
‫This effectively builds an array of questions and their options in questions.
‫Underscore array after the loop completes questions.
‫Underscore array contains all the collected data, and you can work with this array to display, process,
‫or manipulate the information as needed.
‫So for that, I'll use and display the results in Json format echo, Json, underscore, decode or encode
‫function.
‫And we need to display the questions.
‫Underscore array in Json format.
‫Echo is used to output this Json string to the client, and Json underscore encode function is used
‫to convert questions array into a Json string format.
‫Then we need to close the file by this ending tag.
‫Okay, then save as and I'll name it as my quiz API dot php save.
‫And here is our my quiz api dot php file.
‫Congratulations guys!
‫We finished the API.