Page 1 of 1

[SOLVED] conexion php code done but it not work

Posted: Mon Feb 07, 2005 5:11 am
by elcpre
Hello everyone,
I've done several functions shown below that makes mysql connections and return a result. Introducing my website, I made this code in order to announce the 5 latest "properties" registered in my website. It's possible due to Select * From $table LIMIT $num, 5;
$num would be all rows - 5
So it looks neccesary to get all register number in table. So I made the code
Select * From $table; without LIMIT . THen I got 2 queries. 1 query serves to show the latest 5, and the another one gets all register number.
But it doesnt work because it looks it's not possible to make 2 consults to the same table. So, If I put first the all rows code, it gets all num of rows, but the other one query (LIMIT) doesnt get realised.
THis is all the code:

Code: Select all

<?



function searchdb($retVal) &#123;
	global $database;

	while (list(, $db) = each($database)) &#123;
		switch ($db&#1111;"type"]) &#123;
			case "mysql":
				$query = searchMySQL($db&#1111;"server"], $db&#1111;"username"], $db&#1111;"password"], $db&#1111;"database"], $db&#1111;"table"]); 
				break;
			default:
				printf("Error: %s is not a supported database type.<BR>\n", $database&#1111;x]&#1111;"type"]);
				return $retVal;
                                
		&#125;  

		 $size = count($query);
		 
             for($j = 0; $j < $size; $j++) &#123;
			$retVal&#1111;$j] = array("location" => $query&#1111;$j]&#1111;"location"], "type" => $query&#1111;$j]&#1111;"type"], "pricez" => $query&#1111;$j]&#1111;"pricez"], "price" => $query&#1111;$j]&#1111;"price"], "details" => $query&#1111;$j]&#1111;"details"], "ref" => $query&#1111;$j]&#1111;"ref"], "pic" => $query&#1111;$j]&#1111;"pic"], "link1" => $query&#1111;$j]&#1111;"link1"], "link2" => $query&#1111;$j]&#1111;"link2"]);
		 &#125;
	&#125;  

	return $retVal;
&#125;


       
/*
** Description of arguments to searchX() funcs.
**
** $server     - SQL Server (and port). ie: 'sql.domain.com:3306'
** $username   - Server/Database username.
** $password   - Server/Database password.
** $database   - Database to search.
** $table      - Table to search.
** $rfields    - Array of field(s) to take data from on a match. This data is added to $retVal&#1111;x]&#1111;fieldname].
** $sfields    - Array of field(s) to search for $string within.
** $string     - String to search for within $table.$sfields&#1111;].
*/

function searchMySQL($server, $username, $password, $database, $table) &#123;
	settype($retVal, "array");
       
            
     	if ($config&#1111;"persistent"]) &#123;
		$con = mysql_pconnect($server, $username, $password);
	&#125; else &#123;
		$con = mysql_connect($server, $username, $password);
	&#125;
	if (!$con) &#123;
		echo "Error: Connection to SQL server failed.";
		return $retVal;
	&#125;

	$coner = mysql_select_db($database, $con);
	if (!$coner) &#123;
		echo "Error: Connection to database failed.";
		return $retVal;
	&#125;
          
     $total = count($resultados);
 $num = $total - 5;     
	$statement = "Select location, type, pricez, price, details, ref, pic, link1, link2 FROM $table LIMIT $num,5";
         
      
      if ($config&#1111;"debug"]) &#123;
		printf("Statement: %s<BR>\n", $statement);
	&#125;
      
    

	$query = mysql_query($statement);
	if (!$query) &#123;
		echo failed;
		return $retVal;
	
          &#125;

	$tol = 0;
	while ($line = mysql_fetch_row($query)) &#123;
		
			$retVal&#1111;$tol]&#1111;"location"] = $line&#1111;0];
                 	$retVal&#1111;$tol]&#1111;"type"] = $line&#1111;1];
            	$retVal&#1111;$tol]&#1111;"pricez"] = $line&#1111;2];
                  $retVal&#1111;$tol]&#1111;"price"] = $line&#1111;3];
			$retVal&#1111;$tol]&#1111;"details"] = $line&#1111;4];
             	$retVal&#1111;$tol]&#1111;"ref"] = $line&#1111;5];
            	$retVal&#1111;$tol]&#1111;"pic"] = $line&#1111;6];
                  $retVal&#1111;$tol]&#1111;"link1"] = $line&#1111;7];
            	$retVal&#1111;$tol]&#1111;"link2"] = $line&#1111;8];
                  
                  
               
		
		$tol++;
	&#125;
     
    
	
	

      if ($config&#1111;"persistent"]) &#123;
		mysql_close($con);
	&#125;
     

     
     return $retVal;
 
  
     
  &#125;




function searchdbs($resultados) &#123;
	global $database;

	while (list(, $db) = each($database)) &#123;
		switch ($db&#1111;"type"]) &#123;
			case "mysql":
				$busqueda = searchMySQLs($db&#1111;"server"], $db&#1111;"username"], $db&#1111;"password"], $db&#1111;"database"], $db&#1111;"table"]); 
				break;
			default:
				printf("Error: %s is not a supported database type.<BR>\n", $database&#1111;x]&#1111;"type"]);
				return $resultados;
                                
		&#125;  

		 $total = count($busqueda);
		 
             for($j = 0; $j < $total; $j++) &#123;
			$resultados&#1111;$j] = array("location" => $busqueda&#1111;$j]&#1111;"location"], "type" => $busqueda&#1111;$j]&#1111;"type"], "pricez" => $busqueda&#1111;$j]&#1111;"pricez"], "price" => $busqueda&#1111;$j]&#1111;"price"], "details" => $busqueda&#1111;$j]&#1111;"details"], "ref" => $busqueda&#1111;$j]&#1111;"ref"], "pic" => $busqueda&#1111;$j]&#1111;"pic"], "link1" => $busqueda&#1111;$j]&#1111;"link1"], "link2" => $busqueda&#1111;$j]&#1111;"link2"]);
		 &#125;
	&#125;  

	return $resultados;
&#125;


       
/*
** Description of arguments to searchX() funcs.
**
** $server     - SQL Server (and port). ie: 'sql.domain.com:3306'
** $username   - Server/Database username.
** $password   - Server/Database password.
** $database   - Database to search.
** $table      - Table to search.
** $rfields    - Array of field(s) to take data from on a match. This data is added to $retVal&#1111;x]&#1111;fieldname].
** $sfields    - Array of field(s) to search for $string within.
** $string     - String to search for within $table.$sfields&#1111;].
*/

function searchMySQLs($server, $username, $password, $database, $table) &#123;
	settype($resultados, "array");

	if ($config&#1111;"persistent"]) &#123;
		$conz = mysql_pconnect($server, $username, $password);
	&#125; else &#123;
		$conz = mysql_connect($server, $username, $password);
	&#125;
	if (!$conz) &#123;
		echo "Error: Connection to SQL server failed.";
		return $resultados;
	&#125;

	$conerz = mysql_select_db($database, $conz);
	if (!$conerz) &#123;
		echo "Error: Connection to database failed.";
		return $resultados;
	&#125;
        
            
           
	$general = "Select location, type, pricez, price, details, ref, pic, link1, link2 FROM $table";
         
   
    

	$busqueda = mysql_query($general);
	

	
        $row = 0;
	while ($stei = mysql_fetch_row($busqueda)) &#123;
		
			$resultados&#1111;$row]&#1111;"location"] = $stei&#1111;0];
                 	$resultados&#1111;$row]&#1111;"type"] = $stei&#1111;1];
            	$resultados&#1111;$row]&#1111;"pricez"] = $stei&#1111;2];
                  $resultados&#1111;$row]&#1111;"price"] = $stei&#1111;3];
			$resultados&#1111;$row]&#1111;"details"] = $stei&#1111;4];
             	$resultados&#1111;$row]&#1111;"ref"] = $stei&#1111;5];
            	$resultados&#1111;$row]&#1111;"pic"] = $stei&#1111;6];
                  $resultados&#1111;$row]&#1111;"link1"] = $stei&#1111;7];
            	$resultados&#1111;$row]&#1111;"link2"] = $stei&#1111;8];
                  
                  
               
		
		$row++;
	&#125;
     
    
	if ($config&#1111;"persistent"]) &#123;
		mysql_close($conz);
	&#125;
	

     
     return $resultados;
  
    
     
&#125;
?>


CAn anyone help me please?
Thank you very much.


feyd | help us help you, use the formatting tags we provide! :evil:

Posted: Mon Feb 07, 2005 9:33 am
by feyd
your code is extremely confusing. And I don't see where you actually count the number of rows and return that kind of information to the main search..

btw you can do the last x rows faster if you have something to sort the data by:

Code: Select all

SELECT * FROM `table_name` ORDER BY `some_field` DESC LIMIT 5

Posted: Fri Feb 11, 2005 11:06 am
by elcpre
LOL, I've solved in my own.... I agree with you feyd my code was so confusing, i was using too much unnecessary function. I've done it now and it works ok. Anyway Thank you to have tried.
Regards.