Solved

Parse error: syntax error, unexpected '->'

Posted on 2013-11-11
5
414 Views
Last Modified: 2013-11-11
Im converting some of my code from old style procedure to OOP in order to tidy up the code a bit better, however Im a little confused on my error saying "Parse error: syntax error, unexpected '->'"

The error Im getting is on the line:-
        global $mysqli->query("select `ticket`.*, `job`.*, `joblocation`.* from `ticket` left join `job` on `job`.`serialNumber` = `ticket`.`jobSerialNumber` left join `joblocation` on `job`.`locationID` = `joblocation`.`locationID` where ((" . $this->buildWhereCompany() . ") and (" . $this->buildWhereSite() . ") and (" . $this->buildWherejobManufacturer() . "));");

Open in new window


So the variable $mysqli is declared outside the class, in another class, but it seems to be raising the parse error in the building of the SQL statment.

My code which replicates the issue:-
$mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

class claTicketTable {
  function __construct() {
        global $mysqli->query("select `ticket`.*, `job`.*, `joblocation`.* from `ticket` left join `job` on `job`.`serialNumber` = `ticket`.`jobSerialNumber` left join `joblocation` on `job`.`locationID` = `joblocation`.`locationID` where ((" . $this->buildWhereCompany() . ") and (" . $this->buildWhereSite() . ") and (" . $this->buildWherejobManufacturer() . "));");
  }

  private function buildWhereCompany() {
    $sqlWhere = "(`joblocation`.`company` like \"%\")";
    return $sqlWhere;
  }
  private function buildWhereSite() {
    $sqlWhere = "(`joblocation`.`site` like \"%\")";
    return $sqlWhere;
  }
  private function buildWherejobManufacturer() {
    $sqlWhere = "(`joblocation`.`manufacturer` like \"%\")";
    return $sqlWhere;
  }
  function drawTable() {
  }
}

Open in new window


Can anyone see what Im doing wrong?????
0
Comment
Question by:tonelm54
  • 3
5 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I don't know if you can use 'global' there but you are definitely not assigning the results of $mysqli->query to a variable.  Without assigning it to a variable you can't use the results.

http://us2.php.net/mysqli_query
0
 

Author Comment

by:tonelm54
Comment Utility
Sorry, dont know how I missed that from my example code, it is there....

If I use:-
$resTable = $mysqli->query("select uuid();");

Open in new window


I get "Notice: Undefined variable: mysqli"

If I use:-
$resTable = global $mysqli->query("select uuid();");

Open in new window


I get "Parse error: syntax error, unexpected 'global'"
0
 

Author Comment

by:tonelm54
Comment Utility
I think my issue is with getting access to the variable $mysqli which is created outside my class, but dont know how (or even it if it is possible) to either get access the the variable $mysqli created externally, or how to pass the reference of the variable through as there is no point in creating a new instance.

Thank you
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
Pass the variable into your class constructor:

class claTicketTable {

  var $sqlConn;

  function __construct($mysqli) {
        $this->sqlConn = $mysqli;
  }
}

$mysqli = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
$myClass = new clasTicketTable($mysqli);

Open in new window

Then throughout your class you can access it using the local sqlConn variable:

$this->sqlConn->query("SELECT ...");
0
 

Author Closing Comment

by:tonelm54
Comment Utility
Thank you :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now