我正在尝试从数据库中进行选择但是我得到了这个错误:
解析错误:第46行的C:\ wamp \ www \ Project \ USSD_Misec \ test_USSD2 \ listener.php中的语法错误,意外的'echo'(T_ECHO)
这是我的代码:
<?php
include "func/database.php";
//$db = new Database();
$db =new Database('localhost', 'username', 'password', 'database_name');
$db->connect();
$data = file_get_contents("php://input");
$line = "\n-------------------------------------------------------------\n";
file_put_contents("logs/ussdgw.txt", $data."\n", FILE_APPEND);
$dataDecoded = json_decode($data);
$uri = array_reverse(explode("/", $_SERVER["REQUEST_URI"]));
$usid = $uri[1];
$action = $uri[0];
$defaultmenu = array(
"shouldClose" => false,
"ussdMenu" => "Hello Welcome to Misec..., choose :
1.Enrolment Enquiry
2.Enrolment Date
3.Exit",
"responseExitCode" => 200,
"responseMessage" => ""
);
switch($action)
{
case 'response':
switch($dataDecoded->text)
{
case 1:
$ussdMenu = "1. Enrolment Enquiry";
$q = "SELECT * FROM ussd ORDER BY id DESC";
$r = $db->query($q);
// if we have a result loop over the result
if($db->num_rows($r) > 0){
while($a = $db->fetch_array_assoc($r)){
echo "{$a['id']} wrote $a['name']\n";
}
}
break;
case 2:
$ussdMenu = "2. Enrolment Date";
break;
}
$response = array(
"shouldClose" => true,
"ussdMenu" => $ussdMenu,
"responseExitCode" => 200,
"responseMessage" => ""
);
$r_encoded = json_encode($response);
echo $r_encoded;
break;
case 'status':
$sessions = file_get_contents("logs/sessions.txt");
$sessionsActive = explode("\n", $sessions);
if(in_array($usid, $sessionsActive))
$sessionActive = true;
else
$sessionActive = false;
$end = array(
"sessionActive" => $sessionActive,
"responseExitCode" => 200,
"responseMessage" => ""
);
$endEncoded = json_encode($end);
echo $endEncoded;
break;
case 'start':
echo json_encode($defaultmenu);
file_put_contents("logs/sessions.txt", $usid."\n", FILE_APPEND);
break;
case 'end':
$sessions = file_get_contents("logs/sessions.txt");
$sessionsActive = explode("\n", $sessions);
unset($sessionActive[$usid]);
$sessionsModified = implode("\n", $sessionActive);
file_put_contents("logs/sessions.txt", $sessionsModified);
$end = array(
"responseExitCode" => 200,
"responseMessage" => "Connection ended"
);
$endEncoded = json_encode($end);
echo $endEncoded;
break;
}
答案 0 :(得分:0)
您已将其中一个变量括在大括号中,这是正确的,但不是第二个。更改以下行:
echo "{$a['id']} wrote $a['name']\n";
为:
echo "{$a['id']} wrote {$a['name']}\n";