CodeIgniter Forums
Newb - converting a class to work in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Newb - converting a class to work in CI (/showthread.php?tid=22400)



Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]Mark Middleton[/eluser]
Hi there

I've got a class that I'm trying to get working within CI, and it has it's own connection to the (mysql) DB and such.

I assume the "best" form would be to convert every reference to a DB call to the DB helper functions.

Would it be possible to make a tweak or two in the current class file to get it to work without re-writing all of the DB calls? (I am using MySQL)

Here's the error I get:

Quote:A PHP Error was encountered

Severity: Warning

Message: mysql_query(): supplied argument is not a valid MySQL-Link resource

Here's a snippet of the code:

Code:
<PHP

class Mptt {

    function mptt($link = null)
    {
        $this->link = $link;
        return true;
    }

    function add($parent, $child_num = 0, $misc_data = false)
    {
        if(!is_numeric($parent) || $parent < 0)
        {
        
        
    ....
    
    
        $result = mysql_query($sql, $this->link) or die(mysql_error() . "<br>\nSQL<br>\n" . $sql);
        
        if(mysql_num_rows($result) != 1)
        { // Row must exist.
            return false;
        }
        $parent = mysql_fetch_assoc($result);
        mysql_free_result($result);
        
    ... and so on ...

Thanks for taking a look,

-Mark


Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]cahva[/eluser]
Before you start doing a CI library of that, you might want to check out CI library MPTtree Wink


Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]Mark Middleton[/eluser]
Dear God - genius.

case closed.

(although, I would like to learn the answer in case something similar comes up in the future) - I'm trying to wrap my head around variable scope and object relationships. I'm learning.

thank you!
-Mark


Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]jcavard[/eluser]
Altough I'm not sure, I think you should change
Code:
$this->
to this
Code:
$this->ci->



Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]kgill[/eluser]
You're right you need to read up on how classes work, particularly class level variables and instantiation.

Look at this line:
Code:
$result = mysql_query($sql, $this->link) or die(mysql_error() . "<br>\nSQL<br>\n" . $sql);

This is where it's dying - the reason: $this->link doesn't contain a valid mysql connection. If you looked mysql_query up on php.net's function list you'd see that's what goes in the 2nd param if you use it.

So where's the connection, well if you look at how the class is instantiated
Code:
function mptt($link = null)
    {
        $this->link = $link;
        return true;
    }

It's expecting you to pass it in - unless you're doing a mysql_connect and passing the resource to this class it's not going to work. So the way you would have gotten it to work would be to do a mysql_connect and pass the resource link in or rewrite this function to make a connection itself.


Newb - converting a class to work in CI - El Forum - 09-08-2009

[eluser]Mark Middleton[/eluser]
Thank you so much for your help and information. Much appreciated.

I'm off and running with the other class