Database not selected php 5.3 - php

I have a kind of connection that worked correctly in php 5.2, but now un updating to version 5.3 and it generates the error: "Database not selected". This is my script:
config.php :
$host = 'localhost'
$user = 'root'
$password = ''
$db = 'mydb'
Conf.class.php :
class Conf{
private $_userdb;
private $_passdb;
private $_hostdb;
private $_db;
static $_instance;
private function __construct(){
require 'config.php';
$this->_userdb=$user;
$this->_passdb=$password;
$this->_hostdb=$host;
$this->_db=$db;
}
private function __clone(){ }
public static function getInstance(){
if (!(self::$_instance instanceof self)){
self::$_instance=new self();
}
return self::$_instance;
}
public function getUserDB(){
$var=$this->_userdb;
return $var;
}
public function getHostDB(){
$var=$this->_hostdb;
return $var;
}
public function getPassDB(){
$var=$this->_passdb;
return $var;
}
public function getDB(){
$var=$this->_db;
return $var;
}
}
Db.class.php :
class Db {
private $server;
private $user;
private $password;
private $data_base;
private $link;
private $result;
static $_instance;
private function __construct() {
$this->setConnection();
$this->connect();
$this->result = null;
}
private function setConnection() {
$conf = Conf::getInstance();
$this->server = $conf->getHostDB();
$this->data_base = $conf->getDB();
$this->user = $conf->getUserDB();
$this->password = $conf->getPassDB();
}
private function __clone(){ }
public static function getInstance() {
if (!(self::$_instance instanceof self)){
self::$_instance=new self();
} return self::$_instance;
}
private function connect() {
$link=mysql_connect($this->server, $this->user, $this->password);
if ($link){
mysql_select_db($this->data_base,$link);
}
if (!$link){
die('Can not connect');
}else{
$this->link = $link;
}
}
}
Apparently Db.class class, does not operate the function getInstance () because no gets the data from Conf.class.
I need to change something in Db.class and Conf.class?

Probably a copy/paste issue but your db construct calls setConnection while your method name is setConexion. You also left out the semi colons in your config file.
Otherwise, your code ran fine for me. Might consider adding an error_reporting(E_ALL); and testing from the command line. You probably have an include file path issue and you are just not seeing the errors.

Related

Fatal error: Uncaught TypeError: Cannot assign PDO to property

Hey guys how can i assign PDO to my AbstractRepository class?
I got this error
#Fatal error: Uncaught TypeError: Cannot assign PDO to property #
class DataBase {
private $conn;
public static $instance;
private static $dsn = 'mysql:host=localhost;dbname=db';
private static $username = 'db';
private static $password = 'db';
public function __construct()
{
try {
$this->conn = new PDO(self::$dsn, self::$username, self::$password);
} catch (\PDOException $exception) {
echo 'Problem mit der Datenbankverbindung' . $exception->getMessage();
die();
}
}
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
public function getConnection() {
return $this->getInstance()->conn;
}
}
abstract class AbstractRepository{
protected DataBase $connection;
public function __construct(){
$this->connection = DataBase::getInstance()->getConnection();
}
}
This is how you can return the Value of type PDO
class DataBase extends PDO{
public static $instance;
private static $dsn = 'mysql:host=localhost;dbname=db';
private static $username = 'db';
private static $password = 'db';
public function __construct()
{
try {
parent::__construct = new PDO(self::$dsn, self::$username, self::$password);
} catch (\PDOException $exception) {
echo 'Problem mit der Datenbankverbindung' . $exception->getMessage();
die();
}
}
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}
abstract class AbstractRepository{
protected DataBase $connection;
public function __construct(){
$this->connection = DataBase::getInstance();
}
}

PHP Singleton pattern, fatal error when calling getter

I am pretty new to PHP and try to learn it the OOP way as I have an understanding of it. My problem is I have no idea why I am getting the null error below when I try to get the mysqli connection.
Fatal error: Uncaught Error: Call to a member function getConn() on
null
<?php
class ConnectDB
{
private $conn;
private function __construct()
{
$this->conn = new mysqli('localhost', 'root', 'root', 'gs');
$this->checkConnection();
}
public function getConn()
{
return $this->conn;
}
/**
* #return ConnectDB
*/
public static function getInstance()
{
static $instance = null;
if($instance == null)
{
$instance == new ConnectDB();
}
return $instance;
}
public function checkConnection()
{
if($this->conn->connect_errno)
{
echo "Can't connect to Database: " .mysqli_connect_error();
exit();
}
else
{
echo "Connected!";
}
}
}
$conn = ConnectDB::getInstance()->getConn();
In your getInstance method, where you create the class instance you wrote $instance == new ConnectDB();. Use a single = for assignments.
I don't think that your getInstance method is a singleton at all. You are initializing the variable $instance in every call to null, so you should get a new instance every time.
Try it like this:
class ConnectDB
{
private $conn;
private static $instance = null;
...
public static function getInstance()
{
if(self::$instance == null)
{
self::$instance == new ConnectDB();
}
return self::$instance;
}
...
See if you can make this work:
<?php
class ConnectDB {
private $_connection;
private static $_instance; //The single instance
private $_host = "localhost";
private $_username = "root";
private $_password = "root";
private $_database = "gs";
//
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error(
"Failed to conencto to MySQL: " . mysql_connect_error(),E_USER_ERROR
);
} else{
echo "Connected!";
}
}
private function __clone() { }
public function getConn() {
return $this->_connection;
}
$db = ConnectDB::getInstance();
$mysqli = $db->getConn();
}
?>

PHP: Script showing parsing syntax error

I found an issue I can't seem to find a solution to. I am using a singleton class / method that return a single static PDO object. When I try to declare a static reference to the object (http://i.imgur.com/EhKZuVH.png), I get http://i.imgur.com/jUPMQrO.png . How would I go about fixing this?
Game Class:
<?php
include_once('functions.php');
include_once('database.php');
$_codeRegex = '^([a-zA-Z0-9]{4,7})$';
class Game
{
public $Id = "";
public $Name = "";
private static $connection = Database::Connect();
public function __construct($id, $name)
{
$this->Id = $id;
$this->Name = $name;
}
}
?>
My singleton Class:
<?php
require_once('config.php');
CONST CONNECTION_FORMAT = 'mysql:host=%1$s;dbname=%2$s;charset=utf8';
class Database
{
private static $cont = null;
public function __construct() {
exit('Initialize function is not excessible.');
}
public static function Connect()
{
if (self::$cont == null)
{
try
{
$pdoConstuct = sprintf(CONNECTION_FORMAT, DB_SERVER, DB_NAME);
self::$cont = new PDO($pdoConstuct, DB_USER, DB_PASS);
self::$cont->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
}
catch(PDOException $e) { return false; }
}
return self::$cont;
}
public static function Disconnect()
{
self::$cont = null;
}
}
?>
PHP is not Java where you can define property values like this.
What you can do here is simply put property initialization into the contructor, like this:
private static $connection;
public function __construct($id, $name)
{
$this->Id = $id;
$this->Name = $name;
self::$connection = Database::Connect();
}
When you define a class property in PHP you can not set its default value to instance of a class.

Can not access to the method of another class from a class in php

I have class for database which is like this
class DB {
private static $_instance = null;
private $_pdo ,
$_query ,
$_results,
$_error=false,
$_count=0;
private function __construct() {
try {
$this->_pdo= new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
//echo 'connected';
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance(){
if (!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function get($table,$where){
return $this->action("select *",$table,$where);
}
}
And I have another class from where I need to use the DB class
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function _construct (){
$this->_db = DB::getInstance();
}
public function check($source , $items = array()){
$check = $this->_db->get($rule_value,array($item, '=',$value));
}
My problem is when I run this program I got an error like this
Call to a member function get() on a non-object
What is the reason for this error as I already have an instance of DB class in my constructor .
Your Validate constructor isn't defined correctly, and thus isn't being called.
public function _construct (){
No constructor call means $this->_db is returning null in your check() method. It should simply have another underscore:
public function __construct() {
^
Constructors and Destructors in PHP

php pdo mysql connect - confusing syntax problem

<?php
$mysql_host='mysql1.000webhost.com';
$mysql_dbname='a8130617_skola';
$mysql_username='something';
$mysql_password='something';
class mysql {
try{
public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname",
$mysql_username, $mysql_password);
}
catch(PDOException $e){
echo $e->getMessage();
}
} //ERROR EXCLAMATION MARK HERE???
?>
why does netbeans 6.9.1 consider this to be false syntax?
many thanks
Do you know anything about OOP ?
Class should contain fields and/or methods. You just surrounded a piece of code with class{}. It is not programming.
Read about OOP in PHP - here is manual: http://php.net/manual/en/language.oop5.php
Read it for your own good.
Edit:
I know that following example can make you much lazy but I'll take a shoot and believe you will read more.
Example class for connections can look like:
class Mysql {
protected $_host;
protected $_dbname;
protected $_username;
protected $_password;
protected $_db;
public function __construct($host = null, $dbname = null, $username = null, $password = null)
{
$this->_host = $host;
$this->_dbname = $dbname;
$this->_username = $username;
$this->_password = $password;
}
public function connect()
{
try {
$this->_db = new PDO('mysql:host=' . $this->_host . ';dbname=' . $this->_dbname, $this->_username, $this->_password);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
public function getDb()
{
return $this->db;
}
public function setHost($host)
{
$this->_host = $host;
return $this;
}
public function getHost()
{
return $this->_host;
}
public function setDbname($dbname)
{
$this->_dbname = $dbname;
return $this;
}
public function getDbname()
{
return $this->_dbname;
}
public function setUsername($username)
{
$this->_username = $username;
return $this;
}
public function getUsername()
{
return $this->_username;
}
public function setPassword($password)
{
$this->_password = $password;
return $this;
}
public function getPassword()
{
return $this->_password;
}
}
And example usage:
$mysql = new Mysql('mysql1.000webhost.com', 'a8130617_skola', 'something', 'something');
$mysql->connect();
try{
public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname",
$mysql_username, $mysql_password);
}
catch(PDOException $e){
echo $e->getMessage();
}
Try catch blocks need to be inside a method. But going with that, not sure why you are wrapping this in a class? Your class is a wrapper for an already defined class.

Categories