472,789 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 2115


$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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.