I have one problem, when i´m going to run the app i receive one System.err: no value for anotacoes!
I think my problem is on php code. If someone help me i'll appreciate a lot your help.
(Sorry for my bad english ;) )
<?php
// array for JSON response
$response = array();
$response1 = array();
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("mobimapa"); // db name...
// check for post data
$pid = $_GET['pid'];
// get a product from products table
$result = mysql_query("SELECT id_anotacao FROM processo_anotacoes WHERE id_processo = $pid");
// check for empty result
if (mysql_num_rows($result) > 0) {
//$response["processo_anotacoes"] = array();
$processo_anotacoes = array();
while ($row = mysql_fetch_array($result)){
array_push($processo_anotacoes, $row["id_anotacao"]);
// $processo_anotacoes["id_anotacao"] = $row["id_anotacao"];
// $processo_anotacoes["id_processo"] = $row["id_processo"];
// success
//$response["success"] = 1;
// user node
// $response["processo_anotacoes"] = array();
}
// echo json_encode($processo_anotacoes);
}
$ids = join(', ',$processo_anotacoes);
$result1 = mysql_query("SELECT * FROM anotacoes WHERE id_anotacao IN ($ids)");
if (mysql_num_rows($result1) > 0) {
$response1["anotacoes"] = array();
// check for empty result
while ($row1 = mysql_fetch_array($result1)) {
//$result1 = mysql_fetch_array($result1);
$anotacoes = array();
$anotacoes["id_anotacao"] = $row1["id_anotacao"];
$anotacoes["nome"] = $row1["nome"];
$anotacoes["descricao"] = $row1["descricao"];
// success
$response1["success"] = 1;
// user node
$response1["anotacoes"] = array();
array_push($response1["anotacoes"], $anotacoes);
}
// echoing JSON response
echo json_encode($response1);
}
?>
08-12 17:09:03.308: D/All Products:(806):
{"success":1,"processo":[{"data":"2013-07-17","id_processo":"1","nome":"Processo
1","imagem":"Later"},{"data":"2013-08-04","id_processo":"2","nome":"Processo
2","imagem":"Later"}]} 08-12 17:09:03.518: I/Choreographer(806):
Skipped 110 frames! The application may be doing too much work on its
main thread. 08-12 17:09:29.238: D/Processo clicado(806): 2 08-12
17:09:29.838: D/Single Product Details(806):
{"product":[{"data":"2013-08-04","id_processo":"2","nome":"Processo
2","imagem":"Later"}],"success":1} 08-12 17:09:30.028: D/Anotacoes
Details(806):
{"success":1,"anotacoes":[{"descricao":"teste","id_anotacao":"3","nome":"Anotacao
3"}]}
The problem is with the PHP variable being passed to the 2nd SQL query. This not the complete code; but if you understand it, it will solve your problem:
$anotacaos = array();
while ($row = mysql_fetch_array($result))
array_push($anotacaos, $row["id_anotacao"]);
$ids = join(',',$anotacaos);
$result1 = mysql_query("SELECT * FROM anotacoes WHERE id_anotacao in ($ids)");
while ($row = mysql_fetch_array($result1))
//do something...
Related
I know other people have gotten this error, and have read threads to try and track down the issue, but my code is too different from other cases. There are about 150 records that should be returned from this specific query.
Below is my code that is causing the error:
$sql = "SELECT * FROM donor WHERE DonationAmount = 1000 AND Category = '1' or DonationAmount = 1000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
$data = array(); // create a variable to hold the information
while (($row = mysqli_fetch_array($result, MYSQL_ASSOC)) !== false){
$data[] = $row; // add the row in to the results (data) array
}
I don't know why I'm getting this error!
EDIT: this is what I'm trying to do with said array.
$counter = 2;
$divisorCount = ceil($counter/2);
$forEachCount = 1;
foreach ($data as $row){
$forEachCount++;
$block[] = "<div class='block'>".$row['DisplayName']."</div>\n";
if($forEachCount > $divisorCount){
$forEachCount = 0;
end($block);
$key = key($block);
$block[$key] .= "</div><div class='column'>"; // the insert
}
}
unset($row,$key,$forEachCount,$divisorCount); //cleanup
$output = "<div class='tableContainer'>
<div class='column'>".implode($block)."</div>
</div>";
Replace your code with this, here while loop creating infinite loop, also check query conditions
$sql = "SELECT * FROM donor WHERE (DonationAmount = 1000 AND Category = '1') or (DonationAmount = 1000 AND Category IS NULL) ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
$data = array(); // create a variable to hold the information
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$data[] = $row; // add the row in to the results (data) array
}
The code below should send back an json with informations in a Database.
It takes two parameters grade and subject. The problem ist, when I use parameters are not in the database behind everything works as expected no entry, but if it would get an answer from the database nothing appears. I mean really nothing. The values i need are there i tried this and no errors are logged into the logging file. As server runs apache2 with php5.6.22 on Debian. I don't know what i did wrong. Hopefully someone can help me.
The Code:
case 'get_books':
$grade = $_GET['grade'];
$subject = $_GET['subject'];
$sqlt = "SELECT * FROM book_type WHERE subject=".$subject." AND grade=".$grade;
$sql = mysqli_query($db, $sqlt);
if(!$sql){
print(json_encode(array('response' => 2)));
die();
}
$response = array();
$response['books'] = array();
while($row=mysqli_fetch_assoc($sql)) {
$book = array();
$book['fullname'] = $row ["fullname"];
$book['ISBN'] = $row ["ISBN"];
$book['id'] = $row ["id"];
array_push($response['books'], $book);
}
$response['response'] = "1";
print(json_encode($response));
die();
I think this might be your problem:
array_push($response['books'], $book);
as far as I know you can't push a variable into a specific index of an array since no key is provided for the item being pushed.
It would be better to do this as follows:
case 'get_books':
$grade = $_GET['grade'];
$subject = $_GET['subject'];
$sqlt = "SELECT * FROM book_type WHERE subject=".mysqli_real_escape_string((htmlspecialchars_decode($subject, ENT_QUOTES)))." AND grade=".mysqli_real_escape_string((htmlspecialchars_decode($grade, ENT_QUOTES)));
$sql = mysqli_query($db, $sqlt);
if(!$sql){
print(json_encode(array('response' => 2)));
die('sql failed');
}
$response = array();
$response['books'] = array();
$response['validator'] = 'valid';
$i = 0;
while($row=mysqli_fetch_assoc($sql)) {
$book = array();
$book['fullname'] = $row["fullname"];
$book['ISBN'] = $row["ISBN"];
$book['id'] = $row["id"];
$response['books'][$i] = $book;
$i++;
}
$response['response'] = "1";
var_dump($response);
//echo json_encode($response);
die();
Hi i am querying my db so that i can compare every two rows. ex 1 and 2, then 2 and 3, then 3 and 4. and so on and so forth. but it only compares the first two rows. any ideas? here is my code:
$result = mysql_query("SELECT *FROM attendance ORDER BY pid ASC") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// $response["nominees"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$prev_sid = $row["sid"];
$prev_pid = $row["pid"];
$row2 = mysql_fetch_array($result);
if($row2["pid"] == $prev_pid){
if(($row2["sid"] - $prev_sid) == 1){
$attended_elec = mysql_query("SELECT pid FROM election_attendance where election_id = '$election_id'");
if(mysql_num_rows($attended_elec) > 0){
$not_officer = mysql_query("SELECT usertype FROM users WHERE pid = '$prev_pid'");
if(mysql_result($not_officer, 0) == "member"){
// echo "PID" . $prev_pid;
// $nominee["pid"] = $row2["pid"];
$user_details = mysql_query("SELECT *FROM users WHERE pid = '$prev_pid'");
if(mysql_num_rows($user_details) > 0){
$response["nominees"] = array();
while ($row3 = mysql_fetch_array($user_details)) {
$nominee = array();
$nominee["pid"] = $row3["pid"];
$nominee["firstname"] = $row3["firstname"];
$nominee["lastname"] = $row3["lastname"];
$nominee["gender"] = $row3["gender"];
$nominee["contact"] = $row3["contact"];
$nominee["email"] = $row3["email"];
$nominee["address"] = $row3["address"];
$nominee["institution"] = $row3["institution"];
$nominee["points"] = $row3["points"];
$nominee["usertype"] = $row3["usertype"];
// push single product into final response array
array_push($response["nominees"], $nominee);
}
}
}
}
}
}
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "There is no candidate yet from the records.";
// echo no users JSON
echo json_encode($response);
}
thanks in advance, have a nice day
Theres a problem right at the while(), you're fetching the 1st row, and is correct.
Then, inside it you fetch the 2nd, which is what you intend, but when the iteration is repeating, the while will fetch the 3rd, and the inner the 4th, so you lost there the comparisson between 2nd and 3rd.
// fetch data from DB
$results = "...";
if (mysql_num_rows($result) < 1)
{
// no products found tell your users
return;
}
// lets make some variables to walk through the list
$previous = mysql_fetch_array($results);
$actual = null;
// and now lets walk through the list
while($actual = mysql_fetch_array($results))
{
// execute your logic here
// than at the end move the actual row to become the previous
$previous = $actual;
}
NOTICE you shouldn't use mysql_ * methods they're are deprecated. you can use the brother mysqli_ * or PDO
I would do something like this? But there is almost certainly a less resource intensive way to do it.
$x = 0;
$y = 1;
$total = mysql_num_rows(mysql_query("SELECT * FROM `the_place`");
while ($x < $total AND $y < total){
$query1 = "SELECT * FROM `the_place` LIMIT $x,1";
$query2 = "SELECT * FROM `the_place` LIMIT $y,1";
$row1 = mysql_fetch_array(mysql_query($query1);
$row2 = mysql_fetch_array(mysql_query($query2);
// Do comparison here
if ($row1 == $row2){
// etc
}
$x = $x++;
$y = $y++;
}
The problem I have is when I echo or print the following variables, the data I receive is that of the last business listed in my table only.
At present no matter the listing I click I get the same set of data for the last business returned.
As you can see in the below code I am passing the business_name from the clicked listing to be used in my query to find the relevant business profile information.
$business_name = mysql_real_escape_string($_GET['business_name']);
$query = "SELECT
business_id,
category,
years_recommended,
profile_size,
business_name,
established,
employees,
service,
strengths,
ideal_for,
reassurance
FROM
business_data
WHERE
business_name = '$business_name'
AND
profile_size = 'A'
OR
profile_size = 'B'
OR
profile_size = 'C'
OR
profile_size = 'D'
OR
profile_size = 'E'";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
}
echo...
If you need more information please let me know.
Is there anything wrong with my code?
Many thanks in advance.
Your echo call is outside the fetch loop, so you'll only see the last result even though the others were returned.
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
// Echo **inside** the loop
echo...
}
If you wish, you can store all the results in a large array, which can then be used anywhere subsequently in your script, as many times as needed:
// Array for all results
$results = array();
while($row = mysql_fetch_array($result)) {
// Append each row fetched onto the big array
$results[] = $row;
}
// Now use it as needed:
foreach ($results as $r) {
echo $r['profile_size'];
print_r($r);
}
your echo should be inside the loop
I am using the code below to get information from a database and make it into JSON (it may be wrong).
Unfortunately it won't load in my web browser, it just says it's loading but it doesn't finish. Please can you tell me what I am doing wrong.
$query = mysql_query("SELECT * FROM Posts ORDER BY date DESC") or die(mysql_error());
$array = array();
while ($row = mysql_fetch_assoc($query)) {
$array[] = $row;
$postID = $row['id'];
while ($ra = mysql_fetch_assoc(mysql_query("SELECT * FROM Comments WHERE postID = '$postID'"))) {
$array['comments'] = $ra;
}
while ($rd = mysql_fetch_assoc(mysql_query("SELECT * FROM Likes WHERE postID = '$postID'"))) {
$array['likes'] = $rd;
}
}
echo json_encode($array);
You are executing mysql_query in the infinite loop:
on each iteration you query the database, and fetch the first row. Change it to
$res = mysql_query("SELECT * FROM Comments WHERE postID = '$postID'");
if (!$res)
{
// handle error
}
while ($ra = mysql_fetch_assoc($res))
{
....
}
And the same for your second query.