I need some help.. I have a php script which performs a search on a database based on 3 keywords, and the queries work great on the phpadmin for the database but the returned JSON response finds no result ( "No empresa found" ). can anyone tell me why? If I perform the single table search, It works perfectly. ( In case I just use on keyword associated with one table )
Keyword 1 is for Name on Table A Keyword 2 is for PriceRange on table Prices Keyword 3 is for Sector on Table Sectors
Tables are like:
Table A Table Prices Table Sectors
Name PriceRange Sector ID Pricerange ID Sector
XY 1 2 1 100€ to 200€ 1 Computers
YX 2 1 2 200€ to 300€ 2 Tablets
PHP script
<?php
error_reporting(0);
require_once('db_config.php');
$conn = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
$db= mysql_select_db($db_database, $conn);
header('content-type: application/json; charset=utf-8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
// array for JSON response
$response = array();
// get empresa based on keyword
$keyword=$_GET["keyword"];
$keyword2=$_GET["keyword2"];
$keyword3=$_GET["keyword3"];
if($keyword2 == "Todos os Setores" and $keyword3 == "Qualquer Investimento" ):
$result = mysql_query("SELECT * FROM empresa WHERE marca LIKE '%$keyword%' and ficha LIKE '%1' ") or die(mysql_error());
elseif($keyword2 == "Todos os Setores"):
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa , investimento where investimento.investimento='%$keyword3%' and empresa.nivelinvestimento=investimento.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
elseif($keyword3 == "Qualquer Investimento"):
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa, sector where sector.sector='%$keyword2%' and empresa.sector=sector.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
else:
//query a funcionar
$result = mysql_query("SELECT empresa.ficha, empresa.marca, empresa.empresa, empresa.descricao, empresa.imagempequena from empresa , investimento, sector where investimento.investimento='%$keyword3%' and empresa.nivelinvestimento=investimento.id and sector.sector='%$keyword2%' and empresa.sector=sector.id and empresa.ficha LIKE '%1' and empresa.marca LIKE '%$keyword%'") or die(mysql_error());
endif;
// check for empty results
if (mysql_num_rows($result) > 0) {
// looping through all results
$response["empresa"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$empresa= array();
$empresa["marca"] = $row["marca"];
$empresa["empresa"] = $row["empresa"];
$empresa["descricao"] = $row["descricao"];
$empresa["imagempequena"] = $row["imagempequena"];
// push single empresa array into final response array
array_push($response["empresa"], $empresa);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No empresa found";
// echo no users JSON
echo json_encode($response);
}
?>