468,765 Members | 1,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Why can't I pass a db link into a constructor?

I am trying to pass a connected db link into a class constructor. In
the calling module, the link opens successfully and passes the
is_resource test. If I pass the same link into a class constructor,
inside the constructor it fails the is_resource test.

I am new to php. Why is the act of passing this value fundamentally
altering it? This isn't happening with variables holding text or
numeric values. Am I missing something here?

This is the class...

class HFrame {
var $index;
var $dblink;
var $dbname;

function HFrame ($i, $ln, $db) {
if (!is_resource($db)) {
die('Failed resource check in constructor: ' . mysql_error());
} else {
echo "Passed resource check in constructor<BR>\n";
}
}

This is the calling module...

include_once("hframe.php");
$server = "localhost:3306";
$username = "webuser";
$password = "notthepassword";

echo "Attempting to open database...<BR>\n";
$link = mysql_connect($server, $username, $password);
if(!$link) die ("Could not connect to database.");
if (!is_resource($link)) {
die('Failed resource check : ' . mysql_error());
} else {
echo "Passed resource check in calling
class<BR>\n";
}
echo "Attempting to create a frame...<BR>\n";
$f = new HFrame("1", "$link", "kdatabase");

Jul 17 '05 #1
4 2014


$link = mysql_connect($server, $username, $password);
if(!$link) die ("Could not connect to database.");
if (!is_resource($link)) {
die('Failed resource check : ' . mysql_error());
} else {
echo "Passed resource check in calling
class<BR>\n";
}
echo "Attempting to create a frame...<BR>\n";
$f = new HFrame("1", "$link", "kdatabase");

"$link" is surely casting the resource $link to a string. Try

$f = new HFrame("1", $link, "kdatabase");

Martin
Jul 17 '05 #2
Well, thank you for that reply. I tried removing the quotes as you
said, but the result is still the same.

Are you able to make this work on your system?

Jul 17 '05 #3
thecrow wrote:
function HFrame ($i, $ln, $db) {
if (!is_resource($db)) {
die('Failed resource check in constructor: ' . mysql_error());
} else {
echo "Passed resource check in constructor<BR>\n";
} $link = mysql_connect($server, $username, $password);
$f = new HFrame("1", "$link", "kdatabase");

(1) remove quotes from $link
(2) you have $ln and $db in the wrong order.

either:

$f = new HFrame("1", "kdatabase", $link);

or:

function HFrame ($i, $ln, $db) {
if (!is_resource($ln)) {
^^^
$f = new HFrame("1", $link, "kdatabase");
HTH,
JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #4
Doh. It works now. The quotes were undoubtedly the main problem and
I had introduced the variable error while tinkering with that.

It's late over here. Thanks for your help.

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jan Pieter Kunst | last post: by
5 posts views Thread by Just Me | last post: by
4 posts views Thread by John Salerno | last post: by
6 posts views Thread by scottyman | last post: by
6 posts views Thread by RobKinney1 | last post: by
5 posts views Thread by benben | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.