Jordi_E Posted July 17, 2006 Share Posted July 17, 2006 Hello everybody. I'm newbie in PHP.I am running PHP version 5.1.2 together with Apache version 2.0.52 and mySQL version 4.1 on a Windows XP system.Consider these functions: [color=green]function BOX_exists( $BOX_id ) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() == 1); }function insert_BOX( $BOX_id, $Description) { if (!$this->BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }}[/color]The problem I have is that calling to insert_BOX function, always inserts correctly data on mySQL, but the return value from insert_BOX is sometimes false (not always, sometimes is true). As can be seen, this should be impossible, since data is correctly inserted. I only call insert_BOX once, but what would happen in case insert_BOX is executed twice for some reason? The second call would return false. I feel this is the problem I'm having.I don't know how to handle this problem. I spent many hours without success. I will appreciate any idea or help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/ Share on other sites More sharing options...
Ninjakreborn Posted July 18, 2006 Share Posted July 18, 2006 Try This[code]function BOX_exists( $BOX_id ) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() == 1); }function insert_BOX( $BOX_id, $Description) { if (BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }}[/code]That should make it stop doing that. Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-59929 Share on other sites More sharing options...
Jordi_E Posted July 18, 2006 Author Share Posted July 18, 2006 [quote [email protected] link=topic=100916.msg399230#msg399230 date=1153237012]Try This[code]function BOX_exists( $BOX_id ) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() == 1); }function insert_BOX( $BOX_id, $Description) { if (BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }}[/code]That should make it stop doing that.[/quote]Thanks a lot for your answer. But obviously I use if (!BOX_exists( $BOX_id )) prior inserting a new BOX. If it exists in the Database, I wish not to insert it.So, changing from (!BOX_exists( $BOX_id )) to (BOX_exists( $BOX_id )) is not the solution.Best regards. Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-59975 Share on other sites More sharing options...
redarrow Posted July 18, 2006 Share Posted July 18, 2006 try it this way but only chaged this ($this->num_rows() > 0); ps. i had a go my self got the same result two inserts dont no why theo.i read the code and basickly its saying if theres one insert but the code says if there 1 insert with another one.can not work it out theo sorry.ps i do get the inpression becouse your pulling one function into another that why there a double entry.function BOX_exists( $BOX_id ) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() > 0); }function insert_BOX( $BOX_id, $Description) { if (!$this->BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }}try this ok.[code]function insert_BOX( $BOX_id, $Description) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() > 0); if (!$this->BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }} [/code] Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-59982 Share on other sites More sharing options...
wildteen88 Posted July 18, 2006 Share Posted July 18, 2006 Change this:[code]return ($this->num_rows() == 1); [/code]to this:[code]return (($this->num_rows() == 1) ? true : false);[/code]The change this:[code]if (!$this->BOX_exists( $BOX_id ))[/code]to this:[code]if ($this->BOX_exists( $BOX_id ) === true)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-60004 Share on other sites More sharing options...
Jordi_E Posted July 18, 2006 Author Share Posted July 18, 2006 [quote author=redarrow link=topic=100916.msg399285#msg399285 date=1153242888]try it this way but only chaged this ($this->num_rows() > 0); ps. i had a go my self got the same result two inserts dont no why theo.i read the code and basickly its saying if theres one insert but the code says if there 1 insert with another one.can not work it out theo sorry.ps i do get the inpression becouse your pulling one function into another that why there a double entry.function BOX_exists( $BOX_id ) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() > 0); }function insert_BOX( $BOX_id, $Description) { if (!$this->BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }}try this ok.[code]function insert_BOX( $BOX_id, $Description) { $sql = "SELECT IDBOX FROM box ". "WHERE IDBOX='".$BOX_id."';"; $this->query($sql); return ($this->num_rows() > 0); if (!$this->BOX_exists( $BOX_id )) { $sql = "INSERT INTO box (IDBOX, Description )". "VALUES ('$BOX_id', '$Description');"; $this->query( $sql ); return true; } else { return false; }} [/code][/quote]Thanks for your answer. I tried what you suggest, but it still behaves bad. Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-60053 Share on other sites More sharing options...
Jordi_E Posted July 18, 2006 Author Share Posted July 18, 2006 [quote author=wildteen88 link=topic=100916.msg399309#msg399309 date=1153244586]Change this:[code]return ($this->num_rows() == 1); [/code]to this:[code]return (($this->num_rows() == 1) ? true : false);[/code]The change this:[code]if (!$this->BOX_exists( $BOX_id ))[/code]to this:[code]if ($this->BOX_exists( $BOX_id ) === true)[/code][/quote]Thanks a lot for your ideas.I tried it, but I still have the same problem.On my explanation I forgot to mention that these functions are inside of a class which inherits from another class called DB, which is implementing the query and the Database connection Quote Link to comment https://forums.phpfreaks.com/topic/14876-function-seems-executed-twice-when-called-only-once/#findComment-60062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.