473,785 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("h frame.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 2151


$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
9500
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
9963
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 must be an object instead of
6
2381
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 object from the calling class, but the compliler doesn't like it when I try to provide and overrided contructor. See the code below: Do I have to stick with the standard ComboBox constructor? If so, how else could I share a common reference to...
5
1914
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
5884
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 = @"C:\elements.xml"; XmlTextWriter xml = new XmlTextWriter(path, System.Text.Encoding.UTF8);
6
4903
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
37
3989
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 a function
6
1713
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 repeat the code within that class. But then that class, calls another class and needs that same function back in the parent. How do I keep passing that delegate up the chain? I am getting errors that says: Cannot convert type...
5
1316
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
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.