Jump to content

Recommended Posts

Hello,

I'm working on a PHP script that allows access through the database. I've added 2 public functions so it'd be easier to access from other pages. My code is: 

<?php
// imports all the necessary files - DO NOT REMOVE.
include('gs-config.php');

class gsGlobal {


public function establishConnection() {
	global $configDetails, $securedConnection;
		$secureConnection = new mysqli($configDetails[0], $configDetails[1], $configDetails[2], $configDetails[3]);
		
		if (mysqli_connect_error()) {
   			 die('Connect Error (' . mysqli_connect_errno() . ') '
          		  . mysqli_connect_error());
		}		

					}
					

public function addPost() {
	global $configDetails;
	$arrayedReturn = array($_POST['title'], $_POST['content'], $_POST['category'], $_POST['keywords']);
	
	$obj = new gsGlobal;
	$obj->establishConnection();
 
	$query = "SELECT * FROM news WHERE id=1";
	$result = $secureConnection->query($query);
	$row = $result->fetch_array(MYSQLI_ASSOC);
	echo $row['title'];
	
	}	
	
}


?>

 

However what seems to be the problem is that it doesn't pass the query from the connection, could somebody help me please?

Edited by JarrodNoonan
Link to comment
https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/
Share on other sites

I don't know if this will help or confuse you even more, but this is what I do, it works for me. ;D

 

 

    private $db_host = "localhost";
    private $db_username =  "root";
    private $db_password = "******";
    private $db_name = "your_database";
    
 
     public function __construct()
    {
        $this->database = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name)
                  or die($this->database->error); // Note to self the die function is not going to be used for production website...
    }
 

Then I just use $this->database to connect to the database

 

Even though it's a public constructor, I find that I can still encapsulate my other variables and method /functions using protected  variables and methods/functions throughout my code.

Edited by Strider64

Oh yea, just from personal experience. You will never forgive yourself for not using a static database class. It makes life 1 million times easier. Instead of making it global or instantiating a new db class every single time you want to do a query you simply do DB::runMyQuery("insert query here").

Oh yea, just from personal experience. You will never forgive yourself for not using a static database class. It makes life 1 million times easier. Instead of making it global or instantiating a new db class every single time you want to do a query you simply do DB::runMyQuery("insert query here").

Static methods are very much frowned upon by most people designing OOP applications. Why? Because they promote tight coupling of components which in turn makes code hard to test.

 

While it may seem to simplify things at first, it just makes larger applications harder to wrok with and more prone to bugs.

 

I would never recommend going down the static path, especially for something like a DBAL.

Not to mention it makes connecting to more than 1 database at the time impossible.

Granted, not a problem for the vast majority of cases, but it is worth taking note of it anyway. This is true for all classes you make "static", after all. Since you can't create objects from it, to hold separate data in. While there can be many objects of the class, there is only one class.

Static methods are very much frowned upon by most people designing OOP applications. Why? Because they promote tight coupling of components which in turn makes code hard to test.

 

While it may seem to simplify things at first, it just makes larger applications harder to wrok with and more prone to bugs.

 

I would never recommend going down the static path, especially for something like a DBAL.

 

Everything is frowned upon by someone in the programming world. I would very much suggest using a static db class.

Edited by akphidelt2007
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.