Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

How to define $mysqli once in a class?

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
audiodef
Watchman
Watchman
User avatar
Posts: 6656
Joined: Wed Jul 06, 2005 1:02 pm
Location: The soundosphere
Contact:
Contact audiodef
Website

How to define $mysqli once in a class?

  • Quote

Post by audiodef » Wed Jun 20, 2012 10:47 pm

I have a class that goes like so:

Code: Select all

class Radio{
	function mysqlConnect(){
		global $host,$user,$password,$database;

		$mysqli = new mysqli($host,$user,$password,$database);
		if (mysqli_connect_errno()) {
		    printf("Connect failed: <p>%s</p>", mysqli_connect_error());
		    exit();
		}
		
		return $mysqli;
	}
	function doSomething(){
		$mysqli=$this->mysqlConnect();
		...
		$mysqli->close();
	}
	function doSomethingElse(){
		$mysqli=$this->mysqlConnect();
		...
		$mysqli->close();
	}
	function doYetAnotherThing(){
		$mysqli=$this->mysqlConnect();
		...
		$mysqli->close();
	}
}
I can't help but think there must be a more efficient way to do this - in other words, not have to define $mysqli and then close the connection inside each function. How would I do that?
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Top
Mad Merlin
Veteran
Veteran
Posts: 1155
Joined: Mon May 09, 2005 7:37 pm
Contact:
Contact Mad Merlin
Website

  • Quote

Post by Mad Merlin » Thu Jun 21, 2012 2:08 am

Use a constructor and class members...

Code: Select all

class Radio
{
	private $mysqli;

	function __construct()
	{
		$this->mysqli = new mysqli($host, $user, $password, $database);
	}

	function something()
	{
		$this->mysqli->query(...);
		...
	}

	...
}
The constructor is called once when the object is instantiated, and the member variables are stored in the object.
Game! - Where the stick is mightier than the sword!
Top
audiodef
Watchman
Watchman
User avatar
Posts: 6656
Joined: Wed Jul 06, 2005 1:02 pm
Location: The soundosphere
Contact:
Contact audiodef
Website

  • Quote

Post by audiodef » Thu Jun 21, 2012 1:14 pm

I thought it would have to do with a constructor, but I didn't know exactly how to implement it. Thanks!

I now notice a lot of these:

Code: Select all

Warning: mysqli::close(): Couldn't fetch mysqli in /var/www/synthetronica/htdocs/radio/class_radio.php on line 112 
Unless I remove $this->mysqli->close(); from the end of each function. Also, if the same function is called twice or more in one block, mysqli->close() at the end of the function seems to interfere. Where should mysqli->close() be placed?
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Top
Jeffrey0
n00b
n00b
Posts: 68
Joined: Fri Dec 31, 2004 5:33 am

  • Quote

Post by Jeffrey0 » Fri Jun 22, 2012 2:50 am

audiodef wrote:Also, if the same function is called twice or more in one block, mysqli->close() at the end of the function seems to interfere. Where should mysqli->close() be placed?
PHP automatically closes connections when the mysqli object is garbage collected, so there's no real need to close it manually with exception of a few very special cases.

Otherwise, you'd call it in your __destruct destructor. The destructor gets called when there's no references to your Radio instance left anymore.

Code: Select all

class Demo {
  function __construct() {
    echo 'hi!';
  }
  function __destruct() {
    echo 'bye!';
  }
}

function make_a_demo() {
  $test = new Demo(); // hi!
}
make_a_demo();
// the $test from make_a_demo is gone now - bye! gets printed

$otherTest = new Demo(); // hi!
// end of the script - the destructor gets called here but it might be too late to actually send any text over to the browser
Top
audiodef
Watchman
Watchman
User avatar
Posts: 6656
Joined: Wed Jul 06, 2005 1:02 pm
Location: The soundosphere
Contact:
Contact audiodef
Website

  • Quote

Post by audiodef » Fri Jun 22, 2012 3:14 pm

Thanks, I appreciate that.

I doubt I have any special cases requiring me to call close(), but what would those be?
decibel Linux: https://decibellinux.org
Github: https://github.com/Gentoo-Music-and-Audio-Technology
Facebook: https://www.facebook.com/decibellinux
Discord: https://discord.gg/73XV24dNPN
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Authors
Gentoo is a trademark of the Gentoo Foundation, Inc. and of Förderverein Gentoo e.V.
The contents of this document, unless otherwise expressly stated, are licensed under the CC-BY-SA-4.0 license.
The Gentoo Name and Logo Usage Guidelines apply.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy