Page 1 of 1

is mysqli loosing connection to database???

Posted: Sun Jun 29, 2008 10:12 am
by ddragas
hi all

I have problem with loosing connections when working with mysqli. I've made class for manipulating mysqli queries to work with stored procedures, and discovered that after first execution of query with stored procedure - I can not execute second store procedure !?!?!?

constatly I get error "mysqli_error() expects exactly 1 parameter, 0 given"

mysql server version 5.0.51a-3ubuntu5.1

WHY ???

How can I fix this?

here is whole class

Code: Select all

 
<?php
 
 
 
class MySqliClass
    {
        var $Username;
        var $Password;
        var $Server;
        var $Port;
        var $Database;
        var $Connection;
        var $Result;
        var $MysqlArray;
        var $Query;
        
        function MySqliClass($Server, $username, $Password, $Database, $Port)
            {
                $this->Username = $username;
                $this->Password = $Password;
                $this->Server = $Server;
                $this->Port = $Port;
                $this->Database = $Database;
                $this->MakeConnection();
            }
            
            
        function MakeConnection()
            {
                if((!empty($this->Username)) && (!empty($this->Password)) && (!empty($this->Server)) && (!empty($this->Port)))
                    {
                        $this->Connection = mysqli_connect($this->Server,$this->Username,$this->Password,$this->Database, $this->Port);
                        return $this->Connection;
                    }
            }   
        
        function ExecuteQuery($Query)
            {
                if($this->Connection)
                    {
                        if(!empty($Query))
                            {
                                $this->Query = $Query;
                                $this->Result = mysqli_query($this->Connection, $this->Query) or die (mysqli_error());
                            }
                        if($this->Result)
                            {
                                return $this->Result;
                            }
                                else
                            {
                                return;
                            }
                    }
            }
 
        function GetMysqlArray()
            {
                if($this->Result)
                    {
                        $this->MysqlArray = mysqli_fetch_array($this->Result);
                        return $this->MysqlArray;
                    }
            }
        
        function FreeResult()
            {
                if($this->Result)
                    {
                        mysqli_free_result($this->Result);
                    }               
            }
        
        function CloseConnection()
            {
                if($this->Connection)
                    {
                        mysqli_close($this->Connection);
                    }               
            }
    }
 
 
and usage

Code: Select all

 
$DB = new MySqliClass("hostname",  "username", "password", "tablename", "3306");
$DB->ExecuteQuery("call firstSP('value1')");
while($rez = $DB->GetMysqlArray())
    {
        $r[] = $rez[0];
    }
 
$DB->ExecuteQuery("call secondSP(value2)");
$w = $DB->GetMysqlArray();
 

please if somebody can point me in right direction ???

Thank you

kind regards

ddragas

Re: is mysqli loosing connection to database???

Posted: Sun Jun 29, 2008 10:45 am
by onion2k
mysqli_error() requires a parameter, namely the connection to the database. It can't be used like mysql_error() where the link is optional.

Generally if you get an error with a specific function the first thing to do is read the manual page about that function.

Re: is mysqli loosing connection to database???

Posted: Sun Jun 29, 2008 11:43 am
by ddragas
thank you.

now I get error "Commands out of sync; you can't run this command now"

Now I have added method $DB->FreeResult(); (after executing query) and now II get following error

Couldn't fetch "mysqli_result"

don't understand. http://dev.mysql.com/doc/refman/5.0/en/ ... -sync.html is written that I have to clear memory by this command, and I can not do it.
What am I doing wrong?

code looks like this now

Code: Select all

 
$DB = new MySqliClass("hostname",  "username", "password", "table", "3306");
$DB->ExecuteQuery("call firstSP('value1')");
while($rez = $DB->GetMysqlArray())
    {
        $r[] = $rez[0];
    }
 $DB->FreeResult();
 
$DB->ExecuteQuery("call secondSP(value2)");
$w = $DB->GetMysqlArray();
$DB->FreeResult();
 

Re: is mysqli loosing connection to database???

Posted: Mon Jun 30, 2008 11:40 pm
by pickle
I've got this error a few times now too. Best I can tell is that it's a bug with PHP. I found some posts regarding this bug, but I couldn't find them again.

Which version of PHP are you using? I'm running 5.1.2 - not sure if the latest version fixes the problem.

Re: is mysqli loosing connection to database???

Posted: Tue Jul 01, 2008 1:10 am
by ddragas
unfortunately it doesn't fix it (if this is bug) I'm using latest php version 5.2.6 and latest mysql version mentioned above in my earlier post

Re: is mysqli loosing connection to database???

Posted: Tue Jul 01, 2008 2:22 am
by Benjamin
ddragas wrote:now I get error "Commands out of sync; you can't run this command now"
You need to loop through all the results from the proc. (run cycle_results() method after each proc call)

Code: Select all

 
    public function free_result()
    {
        mysqli_free_result($this->result);
    }
 
    private function store_result()
    {
        return (false === ($this->result = mysqli_store_result($this->link_id))) ? false : true;
    }
 
    public function next_result()
    {
        if (mysqli_next_result($this->link_id) && $this->store_result())
        {
            return true;
        } else {
            $this->result = false;
            return false;
        }
    }
 
    public function cycle_results()
    {
        while ($this->next_result())
        {
            $this->free_result();
        }
    }
 

Re: is mysqli loosing connection to database???

Posted: Tue Jul 01, 2008 2:30 am
by ddragas
thank you for reply

I'll try when I get home

Thank you again

kind regards

ddragas

Re: is mysqli loosing connection to database???

Posted: Wed Jul 02, 2008 3:34 am
by ddragas
works great

thank you