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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thecrow

    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" );

  • Martin Lucas-Smith

    #2
    Re: Why can't I pass a db link into a constructor?



    [color=blue]
    > $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" );[/color]


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

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



    Martin

    Comment

    • thecrow

      #3
      Re: Why can't I pass a db link into a constructor?

      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?

      Comment

      • Jan Pieter Kunst

        #4
        Re: Why can't I pass a db link into a constructor?

        thecrow wrote:
        [color=blue]
        > 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";
        > }[/color]
        [color=blue]
        > $link = mysql_connect($ server, $username, $password);[/color]

        [color=blue]
        > $f = new HFrame("1", "$link", "kdatabase" );[/color]


        (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, <devnull@cauce. org> is a spam trap.
        Real e-mail address unavailable. 5000+ spams per month.

        Comment

        • thecrow

          #5
          Re: Why can't I pass a db link into a constructor?

          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.

          Comment

          Working...