create a php program for the following: There are three doors, one with a valuab
ID: 3830440 • Letter: C
Question
create a php program for the following:
There are three doors, one with a valuable prize and the other two with joke prizes. The player chooses a door that he believes hides the real prize. Once he chooses, one of the doors is open to reveal a joke prize. (The door that is open does not display the real prize nor is the door that the player has chosen.) The player is then offered another choice, to stay with his original choice or switch to the other door that is still unopened. After the player decides, both remaining doors are open to reveal the other joke prize and the real prize. The player wins the prize that is behind the door he ultimately selected. If the player selected the door with the real prize, the player wins.
This game will serve as a computer simulation to calculate the true probabilities for each selection scenario. this game has revealed a peculiar set of probabilities. Most statisticians claim that after the first door is open, the player has a 1 out of 2 chance (50%) of winning the real prize no matter whether he switches doors or not. However, Monty Hall, the show’s original host noticed that the odds of winning the real prize were doubled (2 out of 3 (66%)) if the player switched doors. While this is statistically and logically counter-intuitive, computer simulations indicated that Mr. Hall was correct.
Explanation / Answer
First we will create config.php , which contains database details.
<?php
$user_name="user";
$password="pass";
$mysql:host="localhost";
$db_name="us_states";
?>
To establish db connection, we have to pass various db parameters like:
$con=mysqli_connect($host,$user_name,$password,$db_name);
if($con->connect_error){ die("Connection failed:".$con-->connect_error)
$con ==> will hold handler for db
Now construct sql statements as
$sql="select * from data;";
" put your sql statements within this part ";
Note: we have two semicolon.One for php statement and one for sql statement.
Now to execute sql query we a function in php mysqli_query()
mysqli_query(connection handler,sql query);
$result=mysqli_query($con,$sql);
$result will hold output of sql query
Note:$result may have results from various rows ,it is easier for programmer ,if it in array/json etc formats,to perform various operations.
Function mysqli_fetch_array(query result) ,will fetch result row by row.
row=mysqli_fetch_array($result))
{
array_push($response,array("name"=>$row["name"],"age"=>$row["age"]));
}
this code will put all result in array fomat in $response variable.
If you want it in json, simply use as
json_encode(array("abc"=>$response));
Important :Don't for forgot to close db connection ,after using it
mysqli_close($con);
To perform different operations, create a operation.php
Operation.php has a code to establish db connection + execute sql query +close db connection
we have to call config.php in php to have access to varios db parameters.
like this
require_once 'config.php';
Complete code to display all results.php
<?php
require_once 'config.php';
$con=mysqli_connect($mysql:host,$user,$pass,$us_states,$min_income, $max_income, $min_home_value, $max_home_value);
$sql="SELECT State FROM economic_data WHERE (MedianIncome BETWEEN $min_income AND $max_income) AND (MedianHomePrice BETWEEN $min_home_value AND $max_home_value) ;";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result));
$state=$row[State];
echo $state;
mysqli_close($con);
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.