My project is a webapp that reads a database and sends emails. I inserted the database today and with the "example" one was working okay but did not have so many rows as the real one.
My problem is that something(database side) in one of the queries that I do is making the $results empty.
$selected_category = (isset($_POST['category'])) ? $_POST['category'] : null;
$group = (isset($_POST['group'])) ? $_POST['group'] : null;
if ($selected_category) {
if ($selected_category && $group) {
//para se for selecionado o grupo
if ($selected_category == "-1") {
//para todas as categorias juntas
if ($group == "-1") {
$results = DB::query("SELECT category, name, email FROM clients LIMIT 100");
} else {
$results = DB::query("SELECT category, name, email FROM clients LIMIT %i, 100",$group );
}
} else {
//para as categorias
if ($group == "-1") {
$results = DB::query("SELECT category, name, email FROM clients WHERE category = %i LIMIT 100", $selected_category );
} else {
$results = DB::query("SELECT category, name, email FROM clients WHERE category = %i LIMIT %i, 100", $selected_category, $group );
}
}
error_log(mysql_error());
echo json_encode($results);
} else {
//para se for selecionada a categoria
//se for -1 manda todas as categorias
if ($selected_category == "-1") {
//MAL
$count = DB::query("SELECT COUNT(category) AS cat FROM clients");
$count["category"] = "all";
echo json_encode($count);
//$results = DB::query("SELECT category, name, email FROM clients");
} else {
$count = DB::query("SELECT COUNT(category) AS cat FROM clients WHERE category = %i", $selected_category );
//$results = DB::query("SELECT category, name, email FROM clients WHERE category = %i", $selected_category );
$cat = DB::query("SELECT name FROM categories WHERE super_id = %i", $selected_category);
$count["category"] = $cat;
echo json_encode($count);
}
}
//echo json_encode($results);
}
This is the part where I think the error is. Notice that the variables I change are the $category and to select the limit in the query ($group).
A practicable example is when I make $selected_category = -1 (that means I want all the categories) and I make $group = -1 (the first 100 rows) or $group = 100 (the 100 rows after the 100º row), mysql returns the correct values. But when I make $group = 200 (to select the 100 rows after the row 200º), mysql does not return anything and I get and empty array.
Do any of you have any idea that could be corrupting my data? I have trimmed it already and it did nothing. I'm really stuck and I need to finish this untill next friday or I don't get paid!
Thank you all! And hope someone can help me.
EDIT:
So, I have already did:
1 - changed the database to utf-8 following this answer;
2 - changed the two lines that say latin1
in the meekrodb file to utf8
;
3 - Added the ut8_encode php function to the $results
$json = utf8_encode(json_encode( $results ));
if ( $json === false ) echo "ERROR: " . json_last_error_msg(); else echo $json;
Now I no longer have the utf_8 json error, but $results still come empty.
EDIT2 I passed all of this to the production server and guess what.... it works. Somethings I can't understand yet. Thank you all that spent time with my problem! :)