diff --git a/htdocs/contrat/index.php b/htdocs/contrat/index.php
index 0b43912027b8f0e193463520c6ec36ceb932c75a..c10812f0b1851226fb85b5ae17fdfd9c03c5de27 100644
--- a/htdocs/contrat/index.php
+++ b/htdocs/contrat/index.php
@@ -319,9 +319,10 @@ if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.
 if ($socid) $sql.= " AND s.rowid = ".$socid;
 $sql.= " ORDER BY cd.tms DESC";
 
-if ( $db->query($sql) )
+$resql=$db->query($sql);
+if ($resql)
 {
-	$num = $db->num_rows();
+	$num = $db->num_rows($resql);
 	$i = 0;
 
 	print '<table class="noborder" width="100%">';
@@ -332,7 +333,7 @@ if ( $db->query($sql) )
 	$var=True;
 	while ($i < $num)
 	{
-		$obj = $db->fetch_object();
+		$obj = $db->fetch_object($resql);
 		$var=!$var;
 		print "<tr $bc[$var]>";
 
diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 34516b6cc4f49fd8be0747f1ae40febf8676a80c..591b896b177cdf107611db1472086beeb1caaadf 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -1309,17 +1309,18 @@ class Form
 		$sql .= " ORDER BY a.label ASC";
 
 		dol_syslog("Form::select_address sql=".$sql);
-		if ($this->db->query($sql))
+		$resql=$db->query($sql);
+		if ($resql)
 		{
 			print '<select class="flat" name="'.$htmlname.'">';
 			if ($showempty) print '<option value="0">&nbsp;</option>';
-			$num = $this->db->num_rows();
+			$num = $this->db->num_rows($resql);
 			$i = 0;
 			if ($num)
 			{
 				while ($i < $num)
 				{
-					$obj = $this->db->fetch_object();
+					$obj = $this->db->fetch_object($resql);
 
 					if ($selected && $selected == $obj->rowid)
 					{
@@ -1553,17 +1554,18 @@ class Form
 		$sql .= " WHERE active = 1";
 
 		dol_syslog("Form::select_propal_statut sql=".$sql);
-		if ($this->db->query($sql))
+        $resql=$db->query($sql);
+		if ($resql)
 		{
 			print '<select class="flat" name="propal_statut">';
 			print '<option value="">&nbsp;</option>';
-			$num = $this->db->num_rows();
+			$num = $this->db->num_rows($resql);
 			$i = 0;
 			if ($num)
 			{
 				while ($i < $num)
 				{
-					$obj = $this->db->fetch_object();
+					$obj = $this->db->fetch_object($resql);
 					if ($selected == $obj->id)
 					{
 						print '<option value="'.$obj->id.'" selected="true">';
diff --git a/htdocs/core/class/html.formother.class.php b/htdocs/core/class/html.formother.class.php
index 371d30111a9aa9ae7bd0563b7e6fb812ded56902..10b6d500e3b56461022d39de9eed19195418da5c 100644
--- a/htdocs/core/class/html.formother.class.php
+++ b/htdocs/core/class/html.formother.class.php
@@ -164,17 +164,18 @@ class FormOther
 		$sql.= " WHERE e.active = 1 AND e.fk_pays = p.rowid";
 		$sql.= " ORDER BY pays, e.organization ASC, e.code ASC";
 
-		if ($this->db->query($sql))
+        $resql=$db->query($sql);
+		if ($resql)
 		{
 			print '<select class="flat" name="'.$htmlname.'">';
-			$num = $this->db->num_rows();
+			$num = $this->db->num_rows($resql);
 			$i = 0;
 			print '<option value="-1">&nbsp;</option>'."\n";
 			if ($num)
 			{
 				while ($i < $num)
 				{
-					$obj = $this->db->fetch_object();
+					$obj = $this->db->fetch_object($resql);
 					if ($selected && $selected == $obj->rowid)
 					{
 						print '<option value="'.$obj->rowid.'" selected="true">';
diff --git a/htdocs/expedition/index.php b/htdocs/expedition/index.php
index de659a9c1854b4913d4f7d6164518526110f5b70..29317af118efb8a898652345cae0f3ad9854e182 100644
--- a/htdocs/expedition/index.php
+++ b/htdocs/expedition/index.php
@@ -126,9 +126,10 @@ if ($socid) $sql.= " AND c.fk_soc = ".$socid;
 if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
 $sql.= " ORDER BY c.rowid ASC";
 
-if ( $db->query($sql) )
+$resql=$db->query($sql);
+if ($resql)
 {
-	$num = $db->num_rows();
+	$num = $db->num_rows($resql);
 	if ($num)
 	{
 		$langs->load("orders");
@@ -141,7 +142,7 @@ if ( $db->query($sql) )
 		while ($i < $num)
 		{
 			$var=!$var;
-			$obj = $db->fetch_object();
+			$obj = $db->fetch_object($resql);
 			print "<tr $bc[$var]>";
 			print '<td nowrap="nowrap">';
 			$orderstatic->id=$obj->rowid;
diff --git a/htdocs/fichinter/rapport.php b/htdocs/fichinter/rapport.php
index 7008470ca15148f6896d6d502bdcbec9ebdb913d..8a8abb2d18e71fa55d515d2e7cf6fbce05a99e99 100644
--- a/htdocs/fichinter/rapport.php
+++ b/htdocs/fichinter/rapport.php
@@ -91,7 +91,8 @@ $sql .= " AND datei >= '".$start."' AND datei < '".$end."'" ;
 
 $sql .= " ORDER BY $sortfield $sortorder ";
 
-if ( $db->query($sql) )
+$resql=$db->query($sql);
+if ($resql)
 {
 	$num = $db->num_rows();
 	$title = $langs->trans("Report")." ".dol_print_date(strtotime($start),"%B %Y");
@@ -112,7 +113,7 @@ if ( $db->query($sql) )
 	$DureeTotal = 0;
 	while ($i < $num)
 	{
-		$objp = $db->fetch_object();
+		$objp = $db->fetch_object($resql);
 		$var=!$var;
 		print "<tr $bc[$var]>";
 		print '<td><a href="fiche.php?id='.$objp->fichid.'">'.$objp->ref.'</a></td>\n';
@@ -132,7 +133,7 @@ if ( $db->query($sql) )
 		$i++;
 	}
 	print "</table>";
-	$db->free();
+	$db->free($resql);
 	print "<br>".$langs->trans("Total")." $DureeTotal jour[s]";
 }
 else
diff --git a/htdocs/fourn/commande/index.php b/htdocs/fourn/commande/index.php
index 821c16e2548963520b2870eeb0f68f8d7cfb1da9..4f92c2890f5bac350b86c07728df8e741f7db9c2 100644
--- a/htdocs/fourn/commande/index.php
+++ b/htdocs/fourn/commande/index.php
@@ -127,13 +127,14 @@ if ($conf->fournisseur->enabled)
 	if ($socid) $sql.= " AND c.fk_soc = ".$socid;
 	if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
 
-	if ( $db->query($sql) )
+    $resql=$db->query($sql);
+	if ($resql)
 	{
 		print '<table class="noborder" width="100%">';
 		print '<tr class="liste_titre">';
 		print '<td colspan="2">'.$langs->trans("DraftOrders").'</td></tr>';
 		$langs->load("orders");
-		$num = $db->num_rows();
+		$num = $db->num_rows($resql);
 		if ($num)
 		{
 			$i = 0;
@@ -141,7 +142,7 @@ if ($conf->fournisseur->enabled)
 			while ($i < $num)
 			{
 				$var=!$var;
-				$obj = $db->fetch_object();
+				$obj = $db->fetch_object($resql);
 				print "<tr $bc[$var]>";
 				print '<td nowrap="nowrap">';
 				print "<a href=\"fiche.php?id=".$obj->rowid."\">".img_object($langs->trans("ShowOrder"),"order").' '.$obj->ref."</a></td>";
diff --git a/htdocs/fourn/facture/index.php b/htdocs/fourn/facture/index.php
index 9ac08ea248b6384c8386ff3826ce5327206d9126..f6a21d116e9005b146fe9cba9ea6e4c37c689eec 100644
--- a/htdocs/fourn/facture/index.php
+++ b/htdocs/fourn/facture/index.php
@@ -72,14 +72,15 @@ if ($_POST["mode"] == 'search')
 		$sql.= " WHERE s.nom like '%".addslashes(strtolower($socname))."%'";
 	}
 
-	if ( $db->query($sql) )
+    $resql=$db->query($sql);
+	if ($resql)
 	{
-		if ( $db->num_rows() == 1)
+		if ( $db->num_rows($resql) == 1)
 		{
-			$obj = $db->fetch_object();
+			$obj = $db->fetch_object($resql);
 			$socid = $obj->rowid;
 		}
-		$db->free();
+		$db->free($resql);
 	}
 }
 
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index 2f19b8af8bac5c969d2564a775780b4293e324a8..530a0e31aa834315129c78a40aff6ae419f2443c 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -941,14 +941,15 @@ class Societe extends CommonObject
 		$sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = '".$this->id . "'";
 		$sql .= " AND f.fk_statut = '1' AND f.paye = '0'";
 
-		if ($this->db->query($sql))
+        $resql=$this->db->query($sql);
+		if ($resql)
 		{
-			$num = $this->db->num_rows();
+			$num = $this->db->num_rows($resql);
 			$i = 0;
 
 			while ($i < $num)
 	  {
-	  	$objp = $this->db->fetch_object();
+	  	$objp = $this->db->fetch_object($resql);
 	  	$array_push($facimp, $objp->rowid);
 	  	$i++;
 	  	print $i;
@@ -1441,14 +1442,15 @@ class Societe extends CommonObject
 
 		$sql = "SELECT rowid, email, name, firstname FROM ".MAIN_DB_PREFIX."socpeople WHERE rowid = '".$rowid."'";
 
-		if ($this->db->query($sql) )
+        $resql=$this->db->query($sql);
+		if ($resql)
 		{
-			$nump = $this->db->num_rows();
+			$nump = $this->db->num_rows($resql);
 
 			if ($nump)
 			{
 
-				$obj = $this->db->fetch_object();
+				$obj = $this->db->fetch_object($resql);
 
 				$contact_email = "$obj->firstname $obj->name <$obj->email>";
 
@@ -1828,7 +1830,7 @@ class Societe extends CommonObject
 		if ($resql)
 		{
 			$nump = $this->db->num_rows($resql);
-			$obj = $this->db->fetch_object();
+			$obj = $this->db->fetch_object($resql);
 			$count = $obj->numproj;
 		}
 		else