I stumbled across this site after being slammed hard elsewhere for being a novice and really not knowing what I am doing. What I have read so far is more encouraging. I just wrote my first program in php and it is not working at all right now. All it keeps doing is opening window after window until I force the browser to close. I am using a Mac running Yosemite and using MAMP. Hopefully that is enough background.
I know this is an introduction area, so I will also post this in another forum in case this is closed for being off topic.
This is a login file to connect to the server:
[php]
<?php // login.php // Get connection information echo <<<_ENDhost server
Username
_END $db_server = sanitize_string($localhost); $db_username = sanitize_string($username); $db_password = sanitize_string($password); /* $user = 'root'; $password = 'root'; $db = 'rpsls'; $host = 'localhost'; $port = 3306; $link = mysql_connect( "$host:$port", $user, $password ); $db_selected = mysql_select_db( $db, $link ); */ mysql_connect($db_server, $db_username, $db_password) or die(mysql_error()); // Create rpsls table if it does not exist $tbl = "rpsls"; $query = "CREATE TABLE rpsls(human VARCHAR(10), computer VARCHAR(10), outcome VARCHAR(5), action VARCHAR(15)); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Rock", "Paper", "Lose", "Covers"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Rock", "Scissors", "Win", "Crushes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Rock", "Lizard", "Win", "Crushes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Rock", "Spock", "Lose", "Vaporizes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Paper", "Rock", "Win", "Covers"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Paper", "Scissors", "Lose", "Cuts"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Paper", "Lizard", "Lose", "Eats"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Paper", "Spock", "Win", "Disproves"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Scissors", "Lizard", "Win", "Decapitates"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Scissors", "Spock", "Lose", "Smashes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Scissors", "Rock", "Lose", "Crushes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Scissors", "Paper", "Win", "Cuts"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Lizard", "Spock", "Win", "Poisons"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Lizard", "Rock", "Lose", "Crushes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Lizard", "Paper", "Win", "Eats"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Lizard", "Scissors", "Lose", "Decapitates"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Spock", "Rock", "Win", "Vaporizes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Spock", "Paper", "Lose", "Disproves"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Spock", "Scissors", "Win", "Smashes"); INSERT INTO rpsls (human, computer, outcome, action) VALUES ("Spock", "Lizard", "Lose", "Poisons");"; check_table($tbl, $query); // Create choices table if it does not exist $tbl = "choices"; $query = "CREATE TABLE choices(id SMALLINT, choice VARCHAR(10)); INSERT INTO choices (id, choice) VALUES (1, "Rock"); INSERT INTO choices (id, choice) VALUES (2, "Paper"); INSERT INTO choices (id, choice) VALUES (3, "Scissors"); INSERT INTO choices (id, choice) VALUES (4, "Lizard"); INSERT INTO choices (id, choice) VALUES (5, "Spock");"; check_table($tbl, $query); // Sanitize user input function sanitize_string($var) { $var = stripslashes($var); $var = htmlentities($var); $var = strip_tags($var); return $var; } function check_table($tbl, $query){ $db = new mysqli(...); $result = $db->query("SHOW TABLES LIKE "$tbl); if ($result->num_rows == 0){ mysql_query($query); } } ?>[/php]and this is the program:
[php]<?php
// log into server and database
require_once ‘login.php’;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
$conn = mysql_connect($db_server, $db_username, $db_password) or die(mysql_error());
$db_database = ‘rpsls’;
mysql_select_db($db_database) or die("Unable to select database: " . mysql_error());// Start Game
ready_to_play();
// Rock Paper Scissors Lizard Spock game
function rock_paper_scissors_lizard_spock()
{
$human = human_play();
$computer = computer_play();
game_outcome($human, $computer);
play_again();// Start Game Function
function ready_to_play(){
echo <<<_END
Ready to play Rock, Paper, Lizard, Spock?
Yes | No |
_END
if ($ready == "Yes"){
$query = "CREATE TABLE gameResults (
games SMALLINT NOT NULL,
win SMALLINT NULL,
loss SMALLINT NULL,
draw SMALLINT NULL,
PRIMARY KEY (games))";
mysql_query($query);
rock_paper_scissors_lizard_spock();
}else{
close_rpsls();
}
}
// Play Again
// Start Game Function
function play_again()
{
echo <<<_END
Play Again?
Yes | No |
_END
if ($ready == "Yes"){
rock_paper_scissors_lizard_spock();
}else{
close_rpsls();
}
}
// Human Play Selection
function human_play()
{
echo <<<_END
Let’s Play Rock, Paper, Lizard, Spock
Rock | Paper |
Scissors | Lizard |
Spock | |
_END
return $human;
}
// Computer Play Selection
function computer_play()
{
$play = rand(1,5);
$query = “SELECT choice FROM choices WHERE number = $play”;
$computer = mysql_query($query);
return $computer;
}
// Game Outcome Function
function game_outcome($human, $computer)
{
$win = $loss = $draw = 0
if ($human == $computer){
echo "Draw<br />";
echo "We both played ".$human;
$draw = 1;
}else{
$query = "SELECT outcome, action FROM rpsls WHERE human = $human AND computer = $computer";
$results = mysql_query($query);
$results2 = mysql_fetch_array($results);
$outcome = $results2[0];
$action = $results2[1];
if ($outcome == "Win"{
echo "You Win!!!<br />"
echo "Your ".$human. " ".$action." my ".$computer."<br />";
$win = 1;
}else{
echo "You Lose/.<br />
echo "My ".$computer." ".$action." your ".$human."<br />";
$loss = 1;
}
}
$query = "INSERT INTO gameResults VALUES".(NULL, '$win', '$loss', '$draw')";
mysql_query($query);
}
// Game Statistics Function
function game_statistics ()
{
$query = “SELECT * FROM gameResults”;
$result = mysql_query($query);
$rows = mysql_num_rows($result);
$games = $rows;
$win = $loss = $draw = 0;
for ($index = 0; $index < $rows; ++$index){
$row = mysql_fetch_row($result);
$win = $win + $row[1];
$loss = $loss + $row[2];
$draw = $draw + $row[3];
}
echo <<<_END
Games | Win | Loss | Draw |
$games | $win | $loss | $draw |
_END
}
// Print Statistics and close the game
function close_rpsls(){
echo <<<_END
Are you sure you want to quit?
Yes | No |
_END
if ($ready == "No"){
rock_paper_scissors_lizard_spock();
}else{
$query = "DROP TABLE gameResults";
mysql_query($query);
}
}
// close connection
mysql_close($conn);
?>
[/php]
Please forgive my novice errors and help me figure out what is wrong with this program.
Thank you.
Here is the contents of the error log:
141104 18:36:26 mysqld_safe Starting mysqld daemon with databases from /Applications/MAMP/db/mysql
141104 18:36:28 [Warning] Setting lower_case_table_names=2 because file system for /Applications/MAMP/db/mysql/ is case insensitive
141104 18:36:28 [Note] Plugin ‘FEDERATED’ is disabled.
141104 18:36:28 InnoDB: The InnoDB memory heap is disabled
141104 18:36:28 InnoDB: Mutexes and rw_locks use GCC atomic builtins
141104 18:36:28 InnoDB: Compressed tables use zlib 1.2.3
141104 18:36:28 InnoDB: Initializing buffer pool, size = 128.0M
141104 18:36:28 InnoDB: Completed initialization of buffer pool
141104 18:36:28 InnoDB: highest supported file format is Barracuda.
141104 18:36:32 InnoDB: Waiting for the background threads to start
141104 18:36:33 InnoDB: 5.5.38 started; log sequence number 1711074
141104 18:36:33 [Note] Server hostname (bind-address): ‘0.0.0.0’; port: 8889
141104 18:36:33 [Note] - ‘0.0.0.0’ resolves to ‘0.0.0.0’;
141104 18:36:33 [Note] Server socket created on IP: ‘0.0.0.0’.
141104 18:36:35 [Note] Event Scheduler: Loaded 0 events
141104 18:36:35 [Note] /Applications/MAMP/Library/bin/mysqld: ready for connections.
Version: ‘5.5.38’ socket: ‘/Applications/MAMP/tmp/mysql/mysql.sock’ port: 8889 Source distribution