PHP返回错误代码500.错误在哪里?

时间:2016-07-16 18:19:52

标签: php android sql database android-volley

我有一个Android应用程序,它使用StringRequest来记录用户。我最近更改了一些PHP代码,它现在返回服务器错误500.(我知道错误意味着什么)不幸的是,我找不到错误

我的PHP代码:

<?php
    require("password.php");

    $con = mysqli_connect("mywebsite.com", "username", "password", "dbname");

    $username = $_POST["username"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ?");
    mysqli_stmt_bind_param($statement, "s", $username);
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $colUserID, $colName, $colUsername, $colTheme, $colEmail, $colDefaultRadius, $colPassword, $timeElapsed1, $timeElapsed2, $timeElapsed3, $timeElapsed4, $timeElapsed5, $timeElapsed6, $timeElapsed7);

    $response = array();
    $response["success"] = false;

    while(mysqli_stmt_fetch($statement)){
        if (password_verify($password, $colPassword)) {
            $response["success"] = true;  
            $response["name"] = $colName;
            $response["user_id"] = $colUserID;
            $response["theme"] = $colTheme;
            $response["email"] = $colEmail;
            $response["radius"] = $colDefaultRadius;
            $response["timeElapsed1"] = $timeElapsed1;
            $response["timeElapsed2"] = $timeElapsed2;
            $response["timeElapsed3"] = $timeElapsed3;
            $response["timeElapsed4"] = $timeElapsed4;
            $response["timeElapsed5"] = $timeElapsed5;
            $response["timeElapsed6"] = $timeElapsed6;
            $response["timeElapsed7"] = $timeElapsed7;
        }
    }

    mysqli_stmt_close($statement);

    $statement2 = mysqli_prepare($con, "SELECT * FROM location WHERE username = ?");
    mysqli_stmt_bind_param($statement2, "s", $username);
    mysqli_stmt_execute($statement2);
    mysqli_stmt_store_result($statement2);
    mysqli_stmt_bind_result($statement2, $colUsername, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46, $47, $48, $49);

    while(mysqli_stmt_fetch($statement2)) {
        if($response["success"] == true) {
            $response["1"] = $1;
            $response["2"] = $2;
            $response["3"] = $3;
            $response["4"] = $4;
            $response["5"] = $5;
            $response["6"] = $6;
            $response["7"] = $7;
            $response["8"] = $8;
            $response["9"] = $9;
            $response["10"] = $10;
            $response["11"] = $11;
            $response["12"] = $12;
            $response["13"] = $13;
            $response["14"] = $14;
            $response["15"] = $15;
            $response["16"] = $16;
            $response["17"] = $17;
            $response["18"] = $18;
            $response["19"] = $19;
            $response["20"] = $20;
            $response["21"] = $21;
            $response["22"] = $22;
            $response["23"] = $23;
            $response["24"] = $24;
            $response["25"] = $25;
            $response["26"] = $26;
            $response["27"] = $27;
            $response["28"] = $28;
            $response["29"] = $29;
            $response["30"] = $30;
            $response["31"] = $31;
            $response["32"] = $32;
            $response["33"] = $33;
            $response["34"] = $34;
            $response["35"] = $35;
            $response["36"] = $36;
            $response["37"] = $37;
            $response["38"] = $38;
            $response["39"] = $39;
            $response["40"] = $40;
            $response["41"] = $41;
            $response["42"] = $42;
            $response["43"] = $43;
            $response["44"] = $44;
            $response["45"] = $45;
            $response["46"] = $46;
            $response["47"] = $47;
            $response["48"] = $48;
            $response["49"] = $49;
        }
    }

    echo json_encode($response);
?>

请注意,我知道错误发生在$statement2声明中。我尝试了几件事,但无法修复错误。 $1$49链接到数据库中的varchars。谁能告诉我发生了什么?我尝试过使用Volley Response.ErrorListener并且它最终没有显示任何内容。在此先感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

PHP Docs州:

  

有效的变量名称以字母或下划线开头,后跟   任意数量的字母,数字或下划线。

所以$1$2等在PHP中不是有效的变量名。

要查找实际的错误原因和位置,您还可以检查服务器错误日志的内容(如果可用)(并且PHP配置正确;配置参数error_log必须设置为true)

另外一个提示是在分配绑定结果时使用动态变量名称,因此您可以在循环中分配它们:

而不是

$response["timeElapsed1"] = $timeElapsed1;
$response["timeElapsed2"] = $timeElapsed2;
// and so on

你可以使用

foreach (range(1, 7) as $num) {
   $var = 'timeElapsed'.$num;
   $response['timeElapsed'.$num] = $$var;
}

更紧凑(但更好地评论它,因为它不容易发现)。

答案 1 :(得分:1)

现在允许使用以数字开头的变量名称。也许这就是错误,因为你有很多这样的错误。我建议将它们重命名为$val1,依此类推。

答案 2 :(得分:1)

您绑定的数字变量不合适。更改这些变量可能会影响您的代码...这里是您的代码,变量已更改为使用英文字母:

    <?php
        require("password.php");

        $con = mysqli_connect("mywebsite.com", "username", "password", "dbname");

        $username = $_POST["username"];
        $password = $_POST["password"];

        $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ?");
        mysqli_stmt_bind_param($statement, "s", $username);
        mysqli_stmt_execute($statement);
        mysqli_stmt_store_result($statement);
        mysqli_stmt_bind_result($statement, $colUserID, $colName, $colUsername, $colTheme, $colEmail, $colDefaultRadius, $colPassword, $timeElapsed1, $timeElapsed2, $timeElapsed3, $timeElapsed4, $timeElapsed5, $timeElapsed6, $timeElapsed7);

        $response = array();
        $response["success"] = false;

        while(mysqli_stmt_fetch($statement)){
            if (password_verify($password, $colPassword)) {
                $response["success"] = true;
                $response["name"] = $colName;
                $response["user_id"] = $colUserID;
                $response["theme"] = $colTheme;
                $response["email"] = $colEmail;
                $response["radius"] = $colDefaultRadius;
                $response["timeElapsed1"] = $timeElapsed1;
                $response["timeElapsed2"] = $timeElapsed2;
                $response["timeElapsed3"] = $timeElapsed3;
                $response["timeElapsed4"] = $timeElapsed4;
                $response["timeElapsed5"] = $timeElapsed5;
                $response["timeElapsed6"] = $timeElapsed6;
                $response["timeElapsed7"] = $timeElapsed7;
            }
        }

        mysqli_stmt_close($statement);

        $statement2 = mysqli_prepare($con, "SELECT * FROM location WHERE username = ?");
        mysqli_stmt_bind_param($statement2, "s", $username);
        mysqli_stmt_execute($statement2);
        mysqli_stmt_store_result($statement2);
        mysqli_stmt_bind_result($statement2, $colUsername, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z, $aa, $ab, $ac, $ad, $ae, $af, $ag, $ah, $ai, $aj, $ak, $al, $am, $an, $ao, $ap, $aq, $ar, $as, $at, $au, $av, $aw);

        while(mysqli_stmt_fetch($statement2)) {
            if($response["success"] == true) {
                $response["1"] = $a;
                $response["2"] = $b;
                $response["3"] = $c;
                $response["4"] = $d;
                $response["5"] = $e;
                $response["6"] = $f;
                $response["7"] = $f;
                $response["8"] = $h;
                $response["9"] = $i;
                $response["10"] = $j;
                $response["11"] = $k;
                $response["12"] = $l;
                $response["13"] = $m;
                $response["14"] = $n;
                $response["15"] = $o;
                $response["16"] = $p;
                $response["17"] = $q;
                $response["18"] = $r;
                $response["19"] = $s;
                $response["20"] = $t;
                $response["21"] = $u;
                $response["22"] = $v;
                $response["23"] = $w;
                $response["24"] = $x;
                $response["25"] = $y;
                $response["26"] = $z;
                $response["27"] = $aa;
                $response["28"] = $ab;
                $response["29"] = $ac;
                $response["30"] = $ad;
                $response["31"] = $ae;
                $response["32"] = $af;
                $response["33"] = $ag;
                $response["34"] = $ah;
                $response["35"] = $ai;
                $response["36"] = $aj;
                $response["37"] = $ak;
                $response["38"] = $al;
                $response["39"] = $am;
                $response["40"] = $an;
                $response["41"] = $ao;
                $response["42"] = $ap;
                $response["43"] = $aq;
                $response["44"] = $ar;
                $response["45"] = $as;
                $response["46"] = $at;
                $response["47"] = $au;
                $response["48"] = $av;
                $response["49"] = $aw;
            }
        }

        echo json_encode($response);
    ?>

答案 3 :(得分:1)

错误在变量中。您不能拥有以数字开头的变量。尝试在前面添加一个简单的下划线或小写字母,例如a1-a49。