Open Side Menu Go to the Top
Register
php / mysql help php / mysql help

05-10-2013 , 01:22 AM
doing some homework for a project for my php/sql class. i made this helper class:

Code:
<?php

	// Helper class containing database-related methods. 
	// Does not close links or free results. 
	class SQLHelper
	{
		private $hostName = null;
		private $userName = null;
		private $password = null; 
		private $databaseName = null; 
		private $linkConnection = null;

		// Constructor, takes args used in connecting to a database.
		public function __construct($hostName, $userName, $password)
		{
			$this->hostName = $hostName; 
			$this->userName = $userName; 
			$this->password = $password; 
		}
		
		// Attempts to create and save a link_connection with the passed database name. 
		// Returns true on success, otherwise false. 
		public function ConnectToDatabase($databaseName)
		{
			$this->databaseName = $databaseName; 
			$this->linkConnection = mysqli_connect($this->hostName, $this->userName, $this->password, $databaseName); 
			return ($this->linkConnection == true); 
		}
		
		// Sends the passed query to the current database.
		// Returns the result on success, dies on failure. 
		public function SendQuery($query)
		{
			$retVal = mysqli_query($this->linkConnection, $query) or die("Query failed in ".__METHOD__."()\n"); 
			return $retVal; 
		}
		
		// Uses the currently-connected database and the passed tableName to get the column headers of said table. 
		// Returns an array of column header names on success, dies on failure. 
		public function GetColumnHeaders($tableName)
		{
			if(is_null($this->linkConnection))
			{
				die("Attempt to access database before connecting to it in ".__METHOD__."\n"); 
			}
			
			$query = "SHOW COLUMNS FROM $tableName"; 
			$result = mysqli_query($this->linkConnection, $query) or die("Query failed in ".__METHOD__."()\n"); 
			
			$i = 0; 
			$ar = array(); 
			while ($row = mysqli_fetch_array($result)) 
			{
				$ar[$i++] = $row[0]; 
			}
			
			return $ar; 
		}
	}
?>
we're supposed to use or die, idk if all the time or just a few times to show we know what it does but w/e. My problem is, i need a page which prints out the contents of the entire table and also a page where the user can do a search for items based on the column. These are easy enough when separated, but I dont want to make a new instance of this class for each individual page. For instance, once I populate the headers of each column, i never need to do that again. Once i make an instance of this class i never need to do that again (for the current _session). basically, i want some variables / instances declared when the user FIRST visits index.php. then, i want these variables to be visible and saved between pages and between <?php ?> tags. Any ideas? thanks xD

also, I'm not new to programming but I am new to php and mysql.
php / mysql help Quote
05-10-2013 , 01:52 AM
Create the instance in a separate file where you require_once the file that contains your class. Require_once() the file where you created the instance on your pages.
php / mysql help Quote
05-10-2013 , 02:12 AM
Quote:
Originally Posted by txpstwx
Create the instance in a separate file where you require_once the file that contains your class. Require_once() the file where you created the instance on your pages.
so index.php loads, goes to that required page, an instance of that class is made. then the user clicks a button and goes to view.php, that page sees the require_once, goes to that required page, and an instance of that class is made, no?

say i make an instance of that class on index.php and set a variable within that class. then the user goes to the next page which is view.php, that variable is accessible? i thought i tested that and it didnt work, i guess i'll test it again and make sure.
php / mysql help Quote
05-10-2013 , 02:19 AM
Quote:
Originally Posted by Ryanb9
so index.php loads, goes to that required page, an instance of that class is made. then the user clicks a button and goes to view.php, that page sees the require_once, goes to that required page, and an instance of that class is made, no?
Basically

Quote:
Originally Posted by Ryanb9
say i make an instance of that class on index.php and set a variable within that class. then the user goes to the next page which is view.php, that variable is accessible? i thought i tested that and it didnt work, i guess i'll test it again and make sure.
That won't work.

This will show you how it's usually handled https://github.com/simfatic/Registra.../master/source

https://github.com/simfatic/Registra...membersite.php
https://github.com/simfatic/Registra...ite_config.php
https://github.com/simfatic/Registra...urce/login.php

Last edited by txpstwx; 05-10-2013 at 02:27 AM.
php / mysql help Quote
05-10-2013 , 01:02 PM
05-10-2013 , 01:06 PM
I know one of you guys has a one-liner answer to this question, come on and post it i need it >.<
php / mysql help Quote
05-10-2013 , 03:48 PM
I didn't read your post well enough. I get what you're saying now but I still feel like it would be a lot cleaner if you kept all of your logic in your class.

But yeah, without using post or get to a page with additional logic there isn't really a way to pass data between those pages. I guess you could start a session and store some data in the session but I wouldn't want to do that either.
php / mysql help Quote
05-10-2013 , 03:51 PM
Quote:
Originally Posted by txpstwx
I didn't read your post well enough. I get what you're saying now but I still feel like it would be a lot cleaner if you kept all of your logic in your class.

But yeah, without using post or get to a page with additional logic there isn't really a way to pass data between those pages. I guess you could start a session and store some data in the session but I wouldn't want to do that either.
okay i will just make the instance on each page that needs it. he hasnt even taught classes yet and im sure im the only one thats using them anyway so i doubt this will cost me any points. thanks for your help tho, much appreciated.

if anyone comes up with a solution in the meantime, feel free to post it here.
php / mysql help Quote
05-10-2013 , 04:05 PM
each page request is completely separate, and although this may not be technically true depending on how the web server interacts with PHP, you can think of each request as running a new PHP process.

If you want instances of classes or other variables to persist across requests, you have to explictly use some library that supports that

One option, if you are using PHP's built in sessions, is to store your instance in the $_SESSIONS array. See this

Another option is to use APC, specifically the apc_store() and apc_fetch(), if your PHP includes this module (I think it's fairly standard anymore?)

There's also things like memcache but the previous options are probably easier.

Also, within a single PHP script, all <? ?> blocks have the same scope, so if you define a variable in one block, then close it, you can still refer to that variable in another block later on in the same PHP file.

I suppose I should say given your particular usage (a db helper class), you actually want to be creating a new instance with each page request I would imagine. The various DB APIs (including mysqli) have functionality to re-use connections to the database (which is the only thing you might actually want to persist), but it depends on how you the web server talks to the PHP process.

read this maybe
php / mysql help Quote

      
m