Solved

Parse error: syntax error, unexpected '->'

Posted on 2013-11-11
5
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39638132
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
ID: 39638362
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
ID: 39638388
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 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39638480
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
ID: 39638494
Thank you :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 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