TheAnonymous010 TheAnonymous010 - 4 months ago 10
SQL Question

PHP Returns Error Code 500. Where is the Error?

I have an android app that uses a StringRequest to log a user in. I recently changed some PHP code, and it is now returning a Server Error 500. (I know what the error means) Unfortunately, I cannot find the error.

My PHP code:

<?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);
?>


Please note that I know the error is happening in the
$statement2
statement. I have tried several things, but cannot get the error fixed.
$1
to
$49
are linking to varchars in a database. Can anyone tell me what's going on? I have tried using a Volley Response.ErrorListener and it does not end up showing anything. Thanks in advance for any help!

Answer

PHP Docs state:

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

So $1, $2 etc. are not valid variable names in PHP.

To find about the actual error reason and location, you can also check the contents of the servers error log, if that is available (and PHP is configured appropriately; the configuration parameter error_log has to be set true).

An additional hint would be to use dynamic variable names when assigning the bound results, so you can assign them in a loop:

Instead of

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

you could use

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

which is much more compact (but better comment it because it is not easy to spot).