Solved

Parse error: syntax error, unexpected '->'

Posted on 2013-11-11
5
423 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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

786 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