is mysqli loosing connection to database???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

is mysqli loosing connection to database???

Post 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
Last edited by ddragas on Mon Dec 22, 2008 8:30 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: is mysqli loosing connection to database???

Post 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.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Re: is mysqli loosing connection to database???

Post 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();
 
Last edited by ddragas on Mon Dec 22, 2008 8:35 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: is mysqli loosing connection to database???

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Re: is mysqli loosing connection to database???

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: is mysqli loosing connection to database???

Post 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();
        }
    }
 
Last edited by Benjamin on Tue Jul 01, 2008 3:17 am, edited 1 time in total.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Re: is mysqli loosing connection to database???

Post by ddragas »

thank you for reply

I'll try when I get home

Thank you again

kind regards

ddragas
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Re: is mysqli loosing connection to database???

Post by ddragas »

works great

thank you
Post Reply