Optimise this function

Codes here !

Moderators: egami, macek, gesf

Post Reply
MrOldham101
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Apr 28, 2018 4:00 am

Sat Apr 28, 2018 4:05 am

I have this function, however, I need help optimising it. Please also explain any optimisations so that I know for the future:

Code: Select all

public function myFunction()
{
	$query = sprintf("SELECT * FROM myTable WHERE idcompany=%s", 
	$this->db->GetSQLValueString($_SESSION['cCompany'], "int"));
	$result = $this->db->query($this->db->link, $query) or die($this->db->error($this->db->link));
	$row = $this->db->fetch_assoc($result);
	if($this->db->num_rows($result) > 0)
	{
		do
		{		
			$query = sprintf("INSERT INTO myTable2 (myName, idcompany) VALUES (%s, %s)”, 
			$this->db->GetSQLValueString($_SESSION['cCompany'], "int"),
			$this->db->GetSQLValueString($row[‘name’], "int"));	
			$result = $this->db->query($this->db->link, $query) or die($this->db->error($this->db->link));					
		}
		while($row = $this->db->fetch_assoc($result));				
	}
}

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Sat Apr 28, 2018 6:25 am

optimize for what?

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Mon Jun 04, 2018 7:41 am

Hi,

Looks like you could do all of this with one SQL statement using the INSERT ... SELECT
More at the manual:
https://dev.mysql.com/doc/refman/8.0/en ... elect.html

Hope that helps,
Jon

Post Reply