LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-10-2008, 08:09 AM   #1
sogenant
LQ Newbie
 
Registered: Dec 2007
Posts: 14

Rep: Reputation: 0
php mysqli::connection must stay in scope?


Is it safe/possible to use a mysqli statement object when the connection it came from is out of scope? Can/should I do as below?

Put otherwise: Will the pass-by-ref statement keep the connection alive, or does the connection even have to exist for the statement to work?

(My plan is to put get_rows in a try{}, I'm not really
ignoring error checking)

#main script##################################
get_rows($statement)
$statement->bind_results($yabba, $dabba, $do) ;
while $statement->fetch()
{
#etc.
}
#end of main##################################

function get_rows(&$statement) {
$mysqli = new mysqli(etc);
#etc
$statement->execute();
}
#################end of question###############

Over,
 
Old 07-10-2008, 06:39 PM   #2
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 30
Interesting question. Short answer, from my quick tests, the MySQLi connection object does not need to be in scope for a statement created from it to work, but it does need to exist. Consider the following sample code:
Code:
<?php
function make_stmt() {
	static $ret;
	if (! isset($ret))
		$ret = new mysqli('localhost', 'user', 'pass', 'test');
	$stmt = $ret->prepare('select * from foo where id = ?');
	return $stmt;
}

function get_rows($stmt) {
	$stmt->bind_param('i', $i=1);
	$res = $stmt->execute();
	$ret = $stmt->bind_result($i1, $i2, $i3);
	if ($ret) while ($stmt->fetch()) {
		echo "$i1, $i2, $i3\n";
	}
	
}
$s = make_stmt();
get_rows($s);
As written, this code works just fine, but the MySQLi connection object is clearly out of scope when the statement is executed. However, note that because of the static declaration, the MySQLi object is not destroyed when the make_stmt function exist. If you remove that, then the object is destroyed when the function exits and not only does the statement not work, but the script segfaults when you try to use it.
 
Old 07-10-2008, 08:57 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
The example that AdaHacker gives would make me very nervous in actual implementation, because I would suspect that it is working in a fashion not specifically intended and therefore might change in a future release.

That the static variable retains its value when the function exits should not be taken to mean (in my opinion anyway) that you can use that variable outside of its defined scope; in fact, I would expect that you COULD NOT do so - and AdaHacker might be showing us a bug.

It would be safer and more logical (and clearly in keeping with the intent of the language) to define $ret outside of any function and use the global statement inside of functions that need to refer to $ret.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - mysqli_connect doesn't exist but mysqli is enabled? Splenden Linux - Server 2 04-23-2012 10:39 PM
Php and MySQL Whats this New stuff (Mysqli)? helptonewbie Linux - Newbie 5 04-04-2008 10:59 AM
apache 2.0 + PHP 5 + mysql AND mysqli? nyttoc Linux - Server 1 05-08-2007 08:28 AM
php: scope in ldap_search eantoranz Programming 5 05-02-2006 10:02 AM
scope of an object and global scope lucky6969b Programming 7 12-09-2005 09:09 PM


All times are GMT -5. The time now is 03:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration