drousiodrawr drousiodrawr - 4 months ago 14
MySQL Question

syntax error, unexpected '.' in PHP OOP

I'm working on a simple project and I'm using PHP Object Oriented. Basically I have a class that contains information about how to get data from MySQL database and show them on a page. This class is called

admin.php
and goes like this:

<?php
class Admin{
private $db,$username,$password,$id,$group,$Datetime,$msg;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getAdmin($name)
{
if(!empty($name))
{
$adm = $this->db->prepare("select * from admins where username=?");
$adm->bindParam(1,$name);
$adm->execute();
while($row = $adm->fetch())
{
$this->id = $row['id'];
$this->username = $row['username'];
$this->password = $row['password'];
$this->group = $row['group'];
$this->Datetime = $row['date_joined'];
$this->msg = $row['welcome_message'];
}
}
}
public function getID()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
public function getGroup()
{
return $this->group;
}
public function gtDate()
{
return $this->Datetime;
}
public function welcomeMessage()
{
return $this->msg;
}
}
?>


Then on another page which is called
dashboard.php
,I have included this class file and coded this:

<?php if ($dataSet->welcomeMessage()== 0){
echo "
<li class='dropdown messages-menu'>
<!-- Menu toggle button -->
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>
<i class='fa fa-envelope-o'></i>
<span class='label label-success'>1</span>
</a>
<ul class='dropdown-menu'>
<li class='header'>You have one new message</li>
<li>
<!-- inner menu: contains the messages -->
<ul class='menu'>
<li><!-- start message -->
<a href='message.php?msg=".$dataSet->msg();."'>
<div class='pull-left'>
<!-- User Image -->
<img src='dist/img/user2-160x160.jpg' class='img-circle' alt='User Image'>
</div>
<!-- Message title and timestamp -->
<h4>
Support Team
<small><i class='fa fa-clock-o'></i>Just now</small>
</h4>
<!-- The message -->
<p>Welcome to your admin panel</p>
</a>
</li>
<!-- end message -->
</ul>
<!-- /.menu -->
</li>";
}
?>


But whenever I run it ,I get this error message:

Parse error: syntax error, unexpected '.' on line 15 in dasboard.php

Here's the line 15 of the dasboard file:

<a href='message.php?msg=".$dataSet->msg();."'>


Pretty sure that this must be related to the wrong concatenating and combining a class method within a html tag while I'm echoing out a statement.

So what's the correct way to do this ?

Answer

"$dataSet->msg" is not a function but a property, and you need to call it like this.

<a href='message.php?msg=".$dataSet->msg."'> // but wont work as it is private

or use the method call as in the below line

<a href='message.php?msg=".$dataSet->welcomeMessage()."'>