expiredninja expiredninja - 7 months ago 10
SQL Question

trying to write a basic echo function that also checks a mysql table

I don't understand where the error is coming from but know I am not good at writing MySQL queries, but am not sure if that would throw off the entire function.

php:

function createTable($name, $query)
{
if (tableExists($name))
{
echo "Table '$name' already exists.<br />";
}
}
function createplayer ($name, $allegiance, $element, $age)
{
if (tableExists($name)) {
//Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\xampp\htdocs\mysite\phpscripts\functions.php on line 39

echo "Player $name already exists.<br />";
//Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\mysite\phpscripts\functions.php on line 57
}
else {queryMysql("INSERT INTO players
VALUES'$name', '$age', '$birthdate', '$element', '$allegiance'")
}
}



Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\robotcity\phpscripts\rcfunctions.php on line 61

Answer

Your else statement is out of context without the relavant curly braces:

function createTable($name, $query)
{
    if (tableExists($name))
    {
        echo "Table '$name' already exists.<br />";
    }
}
function createplayer ($name, $allegiance, $element, $age)
{
    if (tableExists($name)) {
        //Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\xampp\htdocs\mysite\phpscripts\functions.php on line 39

        echo "Player $name already exists.<br />";
        //Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\mysite\phpscripts\functions.php on line 57

    // Notice the curly braces around the else
    } else {

        queryMysql("INSERT INTO players    
        VALUES '$name', '$age', '$birthdate', '$element', '$allegiance'"); // and this ;

    }
}

I also noticed you have }; at the end - that ; is not required. You'll also likely run into problems with that query, so better to use single quotes when you're wrapping with double quotes.