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) {
global $database;
while (list(, $db) = each($database)) {
switch ($dbї"type"]) {
case "mysql":
$query = searchMySQL($dbї"server"], $dbї"username"], $dbї"password"], $dbї"database"], $dbї"table"]);
break;
default:
printf("Error: %s is not a supported database type.<BR>\n", $databaseїx]ї"type"]);
return $retVal;
}
$size = count($query);
for($j = 0; $j < $size; $j++) {
$retValї$j] = array("location" => $queryї$j]ї"location"], "type" => $queryї$j]ї"type"], "pricez" => $queryї$j]ї"pricez"], "price" => $queryї$j]ї"price"], "details" => $queryї$j]ї"details"], "ref" => $queryї$j]ї"ref"], "pic" => $queryї$j]ї"pic"], "link1" => $queryї$j]ї"link1"], "link2" => $queryї$j]ї"link2"]);
}
}
return $retVal;
}
/*
** 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їx]їfieldname].
** $sfields - Array of field(s) to search for $string within.
** $string - String to search for within $table.$sfieldsї].
*/
function searchMySQL($server, $username, $password, $database, $table) {
settype($retVal, "array");
if ($configї"persistent"]) {
$con = mysql_pconnect($server, $username, $password);
} else {
$con = mysql_connect($server, $username, $password);
}
if (!$con) {
echo "Error: Connection to SQL server failed.";
return $retVal;
}
$coner = mysql_select_db($database, $con);
if (!$coner) {
echo "Error: Connection to database failed.";
return $retVal;
}
$total = count($resultados);
$num = $total - 5;
$statement = "Select location, type, pricez, price, details, ref, pic, link1, link2 FROM $table LIMIT $num,5";
if ($configї"debug"]) {
printf("Statement: %s<BR>\n", $statement);
}
$query = mysql_query($statement);
if (!$query) {
echo failed;
return $retVal;
}
$tol = 0;
while ($line = mysql_fetch_row($query)) {
$retValї$tol]ї"location"] = $lineї0];
$retValї$tol]ї"type"] = $lineї1];
$retValї$tol]ї"pricez"] = $lineї2];
$retValї$tol]ї"price"] = $lineї3];
$retValї$tol]ї"details"] = $lineї4];
$retValї$tol]ї"ref"] = $lineї5];
$retValї$tol]ї"pic"] = $lineї6];
$retValї$tol]ї"link1"] = $lineї7];
$retValї$tol]ї"link2"] = $lineї8];
$tol++;
}
if ($configї"persistent"]) {
mysql_close($con);
}
return $retVal;
}
function searchdbs($resultados) {
global $database;
while (list(, $db) = each($database)) {
switch ($dbї"type"]) {
case "mysql":
$busqueda = searchMySQLs($dbї"server"], $dbї"username"], $dbї"password"], $dbї"database"], $dbї"table"]);
break;
default:
printf("Error: %s is not a supported database type.<BR>\n", $databaseїx]ї"type"]);
return $resultados;
}
$total = count($busqueda);
for($j = 0; $j < $total; $j++) {
$resultadosї$j] = array("location" => $busquedaї$j]ї"location"], "type" => $busquedaї$j]ї"type"], "pricez" => $busquedaї$j]ї"pricez"], "price" => $busquedaї$j]ї"price"], "details" => $busquedaї$j]ї"details"], "ref" => $busquedaї$j]ї"ref"], "pic" => $busquedaї$j]ї"pic"], "link1" => $busquedaї$j]ї"link1"], "link2" => $busquedaї$j]ї"link2"]);
}
}
return $resultados;
}
/*
** 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їx]їfieldname].
** $sfields - Array of field(s) to search for $string within.
** $string - String to search for within $table.$sfieldsї].
*/
function searchMySQLs($server, $username, $password, $database, $table) {
settype($resultados, "array");
if ($configї"persistent"]) {
$conz = mysql_pconnect($server, $username, $password);
} else {
$conz = mysql_connect($server, $username, $password);
}
if (!$conz) {
echo "Error: Connection to SQL server failed.";
return $resultados;
}
$conerz = mysql_select_db($database, $conz);
if (!$conerz) {
echo "Error: Connection to database failed.";
return $resultados;
}
$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)) {
$resultadosї$row]ї"location"] = $steiї0];
$resultadosї$row]ї"type"] = $steiї1];
$resultadosї$row]ї"pricez"] = $steiї2];
$resultadosї$row]ї"price"] = $steiї3];
$resultadosї$row]ї"details"] = $steiї4];
$resultadosї$row]ї"ref"] = $steiї5];
$resultadosї$row]ї"pic"] = $steiї6];
$resultadosї$row]ї"link1"] = $steiї7];
$resultadosї$row]ї"link2"] = $steiї8];
$row++;
}
if ($configї"persistent"]) {
mysql_close($conz);
}
return $resultados;
}
?>CAn anyone help me please?
Thank you very much.
feyd | help us help you, use the formatting tags we provide!