473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2134


$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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
4
by: John Salerno | last post by:
I thought I might use the XML functions in C# to help me do some repetitive typing in an XHTML file, but I'm stuck. Here's what I have before I just stopped: void WriteXMLFile() { string path...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
6
by: RobKinney1 | last post by:
Hello, This may be an easy question, but I cannot stumle upon the correct way of doing this. I have a function in a base class. I then pass it to a class as a delegate so I don't have to...
5
by: benben | last post by:
How do you declare a function f that takes a parameter that is a pointer to itself? So you can do f(f); Pardon my curiosity! Ben
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.