试图编写一个基本的echo函数,它也检查一个mysql表

时间:2014-06-15 14:48:02

标签: javascript php mysql

我不明白错误的来源,但知道我不擅长编写MySQL查询,但我不确定是否会抛弃整个函数。

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'")
        }
}
  

解析错误:第61行的C:\ xampp \ htdocs \ robotcity \ phpscripts \ rcfunctions.php中的语法错误,意外'}'

2 个答案:

答案 0 :(得分:2)

您尝试将变量$ tableExists作为函数执行。尝试使用

if (tableExists($name)) {
    echo "Player $name already exists.<br />";
} else {
    queryMysql("INSERT INTO players VALUES ('$name', '$age', '$birthdate', '$element', '$allegiance')");
}

如果你有这样的功能。

答案 1 :(得分:1)

你的else语句在没有相关花括号的情况下脱离了上下文:

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 ;

    }
}

我还注意到你最后有}; - 不需要;。您也可能会遇到该查询的问题,因此在使用双引号进行换行时最好使用单引号。