Error while connecting to php server

Asked

Viewed 62 times

2

I’m getting the error while 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)

3 answers

5


Lacked a function prior to the definition of the method get. Without it he confuses the static with a property and waits for a variable.

Follow a simplified working version:

<?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 are starting out, I recommend taking a look at PDO instead of the functions of mysql_*. Has a article that can help you.

  • 1

    Just one detail: he’s using the API mysqli, nay mysql, then there would be no need to exchange for PDO without need. Just commenting, given its addendum at the end of the answer.

  • Dear gmsantos your answer this very correct +1 is already guaranteed, but I have to agree with @Andersoncarloswoss about the end of the answer, has no real advantage between both Apis, so much so that the argument is to migrate from easier bank, this is a mistake, because the syntax of banks are different, perhaps in a ORM system (written in PDO and without queries) it would be advantageous, but woe the own ORM internally it is who would define it. The link of the article you quoted is good, but it is not the case here and neither reinforces anything about the use of PDO > mysqli.

  • I didn’t realize it was mysqli_*, but still prefer to recommend PDO because it abstracts the API from the database communication and has an object-oriented approach. Yes, you can use object-oriented mysqli as well, but it’s still an implementation tied to a specific database.

  • There may be queries that work on one DB or another, but SQL is a default and most queries if written correctly with what it says the specification will work smoothly.

3

Problem is you didn’t set 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;
    }
}

-4

Simple, you put the get separate from Static and Function Before:

public static get() {

Afterward:

public function staticGet() {

recommend that you use the connection VIA: PDO here for the W3C website to know how to make PDO connection:https://www.w3schools.com/php/php_mysql_connect.asp

Don’t forget to set the connection, because you are using it as private and not as pubçic or protected

  • 1

    Dear Jiren, Static is a key word of the language to use calls Estaticas, this is native to the language (many languages included), it makes no sense to join both, there would be no static. I recommend you learn the basics of language before you risk responding and end up teaching people wrong.

  • I’m sorry, but your answer made no sense, neither when you said Static should be together with get, nor when you indicated PDO, much less when you forgot the set.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.