Error connecting to php server

2

I'm getting the error trying to connect to the database.

configuration.php

class Connection {
    private static $connection = null;
    public static get() {
          if (self::$connection == null) { self::$connection = mysqli_connect("mysql.uhserver.com", "user", "passw","db" ); }
          return self::$connection;
    }
}

page php

  header("content-type:application/json");

  require_once '../includes/configuration.php';

  $result= mysqli_query(Connection::get(),"SELECT Id, Titulo, Descricao, date_format(DataEvento, '%d/%m/%Y') AS DataEvento FROM agenda WHERE DATAEVENTO >= NOW() ORDER BY DATACADASTRO" ) or die(mysqli_error());

Error

  

syntax error, unexpected 'get' (T_STRING), expecting variable   (T_VARIABLE)

    
asked by anonymous 29.07.2018 / 15:29

3 answers

5

function was missing before setting get . Without this it confuses static with a property and waits for a variable.

Here's a simplified version that works:

<?php

class Connection 
{
    private static $connection = null;

    public static function get()
    {
          if (self::$connection == null) {
              self::$connection = mysqli_connect("mysql.uhserver.com", "user", "passw","db" );
          }

          return self::$connection;
    }
}

$result = mysqli_query(Connection::get(), "SELECT Id, Titulo, Descricao, date_format(DataEvento, '%d/%m/%Y') AS DataEvento FROM agenda WHERE DATAEVENTO >= NOW() ORDER BY DATACADASTRO" ) or die(mysqli_error());

If you're just getting started, I recommend taking a look at PDOs instead of mysql_* functions. There is a article that can help you. .

    
29.07.2018 / 16:09
3

Problem is that you did not define get() as a function within the class:

class Connection {
    private static $connection = null;
    public static function get() {
    if (self::$connection == null) { 
        self::$connection = mysqli_connect("mysql.uhserver.com", "user", "passw","db" ); }
        return self::$connection;
    }
}
    
29.07.2018 / 16:09
-4

Simple, you have put separate get of static and function Before:

public static get() {

Then:

public function staticGet() {

I recommend that you use the VIA: PDO connection here for the W3C website to learn how to connect PDO: link

Do not forget to put the connection set because you are using it as private and not as public or protected

    
29.07.2018 / 16:11