0

This is a really simple question: Why isn't this program working? I doesn't echo "Connected to database". The problem is that echo doesn't work.

<?php    

  require "Database.php";
  echo "Connected to database";

?>

If this helps a little better, here is Database.php

<?php
 class Database($hostname,$username,$password,$dbname) {

  return new mysqli($hostname,$username,$password,$dbname);

  public static function Query() {

  }
 }
?>
  • 2
    Because you have a syntax error. You cannot just return something (your DB connection) in the middle of a class, you have to put it into a method. Activate [error reporting](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) and you should see the error. – enricog Aug 12 '14 at 06:36

4 Answers4

3

You have an error in your Database.php file.
The error causes the script to exit before the echo, and produce a empty page (or show an error if error reporting is on).

The error is caused by a return statement in the class declaration.
And in-parameters in the class declaration.

A class is declared:

class Database {
  // code.
}

Not

class Database($variable) {

}

And its not possible to return anything from the class itself, it has to be in a function, cause you never actually call the class, as you do with a function, but the class constructor (when you create the class instance using new) and its functions and methods.

Jite
  • 5,761
  • 2
  • 23
  • 37
  • Also, can't use `return new mysqli($hostname,$username,$password,$dbname);` in a class directly. You could use a method – Linga Aug 12 '14 at 06:39
1

There are two errors in the code :

1.Class dont need parameters. If you need parameters then you can use the constructor.

function __construct($hostname, $username, $password, $dbname) {
  return new mysqli($hostname, $username, $password, $dbname);
}

2.Any return can only be used with a method/function. It cannot exist on its own within the Class.

function __construct($hostname, $username, $password, $dbname) {
    return new mysqli($hostname, $username, $password, $dbname); // See here.
}

Please use the rectified code below

<?php
class Database{

protected static $_instance;
protected $_mysqli;

function __construct($hostname,$username,$password,$dbname) {
    $this->_mysqli = new mysqli($hostname,$username,$password,$dbname);
    self::$_instance = $this;
}

public static function Query() {
}
}
?>
Community
  • 1
  • 1
Sovik
  • 59
  • 2
  • Note: Returning something in the constructor won't return a `mysqli` instance, it would still return a `Database` instance and should therefore not be done. – enricog Aug 12 '14 at 06:58
  • I have added some codes, that may help you. For returning the mysqli array, we have to override the self Object of the Database class.It might solve your problem. – Sovik Aug 12 '14 at 07:16
0

First, you need to wrap your require() in parentheses. Second, there is an error in your database.php file. you might want to call the die() function to make sure the code stop executing. Do some debugging on the database.php file.

<?php    

  require("Database.php");
  echo "Connected to database";

?>

Suggestion: its bad practice to name your php files and functions starting with an uppercase letter.

iammikerodriguez
  • 379
  • 1
  • 5
  • 16
0

Also as your question has already been answered I'll give you a helping hand. There is a better alternative to mysqli being dpo, it's mainly better as it supports a variety of DBMS's but it is just better all-round comparatively to mysqli and in my opinion is more secure. Here is some code to connect to a database. So you should note that this same code would work with other databases so there is no need to change your code if there is a need to change to another DBMS/RDMS

define('DB_URL', 'mysql:host=$hostname;dbname=$dbname');

define('DB_USERNAME', $username);

define('DB_PASSWORD', $password);

$db = new PDO(DB_URL, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

Also just a little helping hand for you:

function dp($something, $exit=true)
{
    print('<pre>');
    print_r($something);
    print('</pre>');
    if($exit){
        exit;
    }
}

that will help ALOT with debugging, whenever you need to know the value of a variable within your php, just put

dp($variable, false)

The false is optional, if you add it, then it will continue to load the page, if you leave it out the function will defult to true and it will not load the rest of the page after the dp.

Hope that helps some :)

Joel

Joel
  • 278
  • 2
  • 5
  • 10
  • 2
    Since when is the `mysqli` API outdated? I personally prefer `PDO` also, but `mysqli` is totally okay to use, the `mysql_*` API on the other hand, is NOT okay. :P – Jite Aug 12 '14 at 06:46