简体   繁体   English

解析错误:语法错误,意外的T_VARIABLE,预期为T_FUNCTION

[英]Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION

Could someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

I want to display the users online on specific rooms only. 我只想在特定房间在线显示用户。

the code below is the function that calls my online.php this is under my chat.php when I load the page this function also loads. 下面的代码是调用我的online.php的函数,当我加载该函数的页面时,该函数位于chat.php下。

function whos_online() {
  if ( window.XMLHttpRequest ) {
    xmlhttp = new XMLHttpRequest();
  } else { 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET", "online.php?room=<?php $_SESSION['room']?>", false);
  xmlhttp.send();
  document.getElementById("whos_online").innerHTML = xmlhttp.responseText; 
}

ONLINE.PHP ONLINE.PHP

this is the content of my online.php 这是我的online.php的内容

<link rel="stylesheet" type="text/css" href="style.css" />
<?php

    session_start();
    include 'db.inc.php';

    class WhosOnline{
        $rn = $_GET['room'];
        protected $get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$rn}'";
        public function DisplayUsers(){
            $get_current_status = mysql_query( $this->get_status_query );
            if( mysql_num_rows( $get_current_status ) != 0 ) {
                while( $row_status = mysql_fetch_array( $get_current_status ) ) {
                    if( $_SESSION['username'] == true ) {
                        echo "<div class='online_margin'>
                                <b>".base64_decode($row_status['username'])."</b>
                              </div>
                              <hr style='border: 0; border-top: solid 1px #D8D8D8;margin: 5px 10px 5px 10px;' />";
                    }
                }
            }
        }
    }

    $Online = new WhosOnline;
    $Online->DisplayUsers();
?>

Any help? 有什么帮助吗?

 $rn = $_GET['room']; protected $get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$rn}'"; 

This is a bad habit that you need to break RIGHT NOW . 这是一个坏习惯,您需要立即改正

protected function get_status_query($rn) {
  return "SELECT * FROM `online_users` WHERE `room` =     '". sanitize($rn) . "'";
};

Implementation of sanitize() is left to the reader. sanitize()实现留给读者。

you could not initialized any variable directly in class , try this 您不能直接在类中初始化任何变量,请尝试此操作

public $rn;
protected $get_status_query;

public __construct(){
      $this->rn = $_GET['room'];
      $this->get_status_query = "SELECT * FROM `online_users` WHERE `room` = '{$this->rn}'";
}

Ok, even this gives an error: 好的,即使这样也会产生错误:

class WhosOnline{
    public $rn = $_GET['room'];
}

This also gives an error: 这也会产生一个错误:

$v = "Hi there";
class WhosOnline{
    public $rn = $v;
}

The error is because you're trying to set a variable based on another variable in the class definition. 该错误是因为您试图基于类定义中的另一个变量来设置一个变量。 You could do this in the constructor. 您可以在构造函数中执行此操作。 Or you can set class members based on CONSTANTS (as you were doing with the query string). 或者,您可以基于CONSTANTS设置类成员(就像处理查询字符串一样)。 But why not rewrite your WhosOnline method like this: 但是为什么不这样重写WhosOnline方法:

public function DisplayUsers(){
    $get_current_status = mysql_query(
        "SELECT * FROM `online_users` WHERE `room` = '" 
            . mysql_real_escape_string($_GET['room']) . "'");
    if(mysql_num_rows($get_current_status)!=0){
        while($row_status = mysql_fetch_array($get_current_status)){
            if($_SESSION['username']==true){
                echo "<div class='online_margin'>   <b>".base64_decode($row_status['username'])."</b></div><hr style='border: 0; border-top:  solid 1px #D8D8D8;margin: 5px 10px 5px 10px;' />";
            }
        }
    }
}

This will also remove any potential errors you might have with $this-> references missing. 这也将消除缺少$this->引用时可能出现的任何潜在错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误:语法错误,意外\\&#39;?&gt; \\&#39;,期待函数(T_FUNCTION) - Parse error: syntax error, unexpected \'?>\', expecting function (T_FUNCTION) 解析错误:语法错误,意外的“,”,期望变量(T_VARIABLE) - Parse error: syntax error, unexpected ',', expecting variable (T_VARIABLE) 语法错误,意外的T_FUNCTION,预期为&#39;)&#39; - syntax error, unexpected T_FUNCTION, expecting ')' 意外的T_VARIABLE,期待T_FUNCTION修复 - Unexpected T_VARIABLE, expecting T_FUNCTION fix 意外的 T_VARIABLE,期待 T_FUNCTION - unexpected T_VARIABLE, expecting T_FUNCTION Yii解析错误:语法错误,意外的&#39;if&#39;(T_IF),期望的函数(T_FUNCTION) - Yii Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) Wordpress-解析错误:语法错误,意外的“函数”(T_FUNCTION),期望的标识符(T_STRING) - Wordpress - Parse error: syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) PHP,解析错误:语法错误,意外的T_IF,预期为T_FUNCTION - PHP, Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION PHP解析错误:语法错误,意外的T_STRING,预期的T_FUNCTION - PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION php解析错误:语法错误,意外的T_STRING,在构造上需要T_FUNCTION - php Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on construct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM