Json_encode not print anything - php

I'm building a PHP page, that return a list of items from my database. So I can't see any record because I think that the script go in overflow.
This is my script
<?php
ini_set('memory-limit','-1');
set_time_limit(0);
require_once('lib/connection.php');
$query_Articolo = "SELECT CodArticolo,NomeArticolo,Quantita,CodiceBarre, PrezzoAttuale, PrezzoRivenditore,PrezzoIngrosso
FROM VistaArticoli";
$result_Articoli = $connectiondb->query($query_Articolo);
//I HAVE JUST INSERT THIS LINE CODE
$answer[] =array();
while($row_Articoli = $result_Articoli->fetch_assoc()) {
$answer[] =array("id"=>$row_Articoli['CodArticolo'],"nome"=>$row_Articoli['NomeArticolo'],
"quantita"=>$row_Articoli['Quantita'],"codiceBarre"=>$row_Articoli['CodiceBarre']
,"codartFornitore"=>$row_Articoli['CodiceBarre'], "PrezzoAttuale"=>$row_Articoli['PrezzoAttuale'],
"prezzoRivenditore"=>$row_Articoli['prezzoRivenditore'],"prezzoIngrosso"=>$row_Articoli['prezzoIngrosso']);
}
//echo "risposta";
echo json_encode($answer);
?>
If I try tu call this page, I can't see any record. If I add at the query "limit 500" I can see the result.
How can I change the code?

Related

While-function for mysqli_fetch_assoc is infinite

I have two HTML pages inside a session. On my first page, I want to create an array, which is saved in a $_SESSION variable. On the second page, I want to display my array.
PHP code for my first page looks like that:
<?php
include ("../script/db_connect.php");
$select_questions = 'select * from questions ';
if (isset($_POST["own"]) && $_POST["own"] == "No") {
$select_questions .= 'where creator != '
. $_SESSION["id"];
}
$select_questions .= 'limit 3';
$questions_result = mysqli_query($con, $select_questions);
$_SESSION["questions"] = mysqli_fetch_assoc($questions_result);
mysqli_close($con);
?>
PHP code on my second page looks like that:
<?php while ($array = $_SESSION["questions"]) {
echo $array["question"]; } ?>
When I go to second page, an infinite loop is displayed, where only the first element of my array is displayed over and over again. What is the reason for that? I cannot find any mistake in my code.
Change this
$_SESSION["questions"] = mysqli_fetch_assoc($questions_result);
to (here we fetch all rows from the sql result, not only one)
while($row=mysqli_fetch_assoc($questions_result)) $_SESSION["questions"][] = $row;
And later do
foreach($_SESSION["questions"] as $quest) { echo $quest["question"]; }

ajax returns success but mysql db not updated

I have a javascript for loop that sends an array to an ajax page to update the mysql database.
I echo the result back to the original page and it echos as a success but when I check the db nothing has changed
my javascript for loop that sends the array
for(var m=0; m<array.length; m++){
$.post("update_page_positions.php",{page_ref:array[m][0], ref:array[m][12], menu_pos:array[m][1], sub_menu_pos:array[m][2], top_menu:array[m][3], pagelink:array[m][4], indexpage:array[m][5], hidden:array[m][6], page_title:array[m][7], page_desc:array[m][8], page_keywords:array[m][9], page_name:array[m][10], deletedpage:array[m][11]},
function(data,status){
alert("data="+data+" status="+status);
});
here is the php ajax page the updates the db
<?
include("connect.php");
$ref = $_POST['ref'];
$page_ref = $_POST['page_ref'];
$menu_pos = $_POST['menu_pos'];
$sub_menu_pos = $_POST['sub_menu_pos'];
$top_menu = $_POST['top_menu'];
$indexpage = $_POST['indexpage'];
$page_name = $_POST['page_name'];
$page_title = $_POST['page_title'];
$page_desc = $_POST['page_desc'];
$page_keywords = $_POST['page_keywords'];
$hidden = $_POST['hidden'];
$pagelink = $_POST['pagelink'];
$deletedpage = $_POST['deletedpage'];
$query = mysql_query("SELECT * FROM pages WHERE ref='$ref' AND page_ref='$page_ref'");
if(mysql_num_rows($query)==0){
mysql_query("INSERT INTO pages(page_ref, ref, page_name, menu_pos, sub_menu_pos, top_menu, link, indexpage) VALUES('$page_ref','$ref','$page_name','$menu_pos','$sub_menu_pos','$top_menu','$pagelink','$indexpage')");
}
if($deletedpage=="1"){
mysql_query("DELETE FROM pages WHERE ref='$ref' AND page_ref='$page_ref'");
mysql_query("DELETE FROM site_content WHERE ref='$ref' AND page_ref='$page_ref'");
}
else{
if(mysql_query("UPDATE pages SET menu_pos='$menu_pos', sub_menu_pos='$sub_menu_pos', top_menu='$top_menu', indexpage='$indexpage', page_name='$page_name', page_title='$page_title', desc1='$page_desc', keywords_list='$page_keywords', hidden='$hidden', link='$pagelink' WHERE ref='$ref' AND page_ref='$page_ref'")){
echo "updated!";
} else{
echo "error";
}
}
?>
the INSERT and DELETE functions are fine but the UPDATE returns a success statement but does not update the db.
Can anyone see what the problem is?
Posted as an answer because the comment was too hard to read:
Rather than echoing "updated", try echoing
"UPDATE pages SET menu_pos='$menu_pos', sub_menu_pos='$sub_menu_pos', top_menu='$top_menu', indexpage='$indexpage', page_name='$page_name', page_title='$page_title', desc1='$page_desc', keywords_list='$page_keywords', hidden='$hidden', link='$pagelink' WHERE ref='$ref' AND page_ref='$page_ref'"
(ie. the query you're trying to run).
See if that gives you some clues.
UPDATE reports success, but does nothing in case when its WHERE clause rejects all rows in updated table.
Maybe $page_ref identifier is correct (so DELETE works), but full $page_ref and $ref combination is not?

MYSQLI Returns only 1 value while there's 2

I've started making a Video script that loads the videos using MySql and I am using Mysqli.
However, There's 2 Rows that it should post, but it only post the second none, not the first one.
It loads the results using "Brand" so if there's 2 rows named "Test", it only loads the second one, but not the first one.
So, what is causing this? I've tried with 3 rows, and it did not include the first row.
Code
<?php
{ /* Global Data */
ini_set('display_errors', 0);
ini_set('error_reporting', -0);
$GetPath = $_GET['b'];
$SqlUser = "root";
$SqlPass = "**Private**";
$SqlHost = "localhost";
$SqlData = "heisteknikk";
}
{ /* Mysql Connect */
$Sql = new mysqli($SqlHost, $SqlUser, $SqlPass, $SqlData);
if ($Sql->connect_error) { die("Sorry, Could not connect (".$Sql->connect_errno.") ".$Sql->connect_error);}
}
{ /* Test */
$Brand = $Sql->real_escape_string($GetPath);
$SqlData = "SELECT * FROM videos WHERE Brand = '".$Brand."'";
$SqlQuery = $Sql->query($SqlData);
if (!$SqlQuery) {
echo $Sql->error;
}
if ($SqlQuery->num_rows == 0) {
die("Nothing was found");
}
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {
echo "
<tr><td>".$Heis['Brand']."</td><td>".$Heis['Name']."</td><td>".$Heis['Location']."
";
}
echo "</table>";
$Sql->close();
}
?>
This line causes the bug:
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
With this line you effectively throw out the first row of the result set, as you don't process $Data at all in the code below. Just remove it, and your script should work fine (I assume that closing </td></tr> sequence was lost when pasting the code, am I right?)
Comment out (or better yet, remove) this line:
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
It is grabbing the first row...then you aren't doing anything with $Data and then grabbing the second (and consecutive rows) for display in your while loop. Commenting out that line above will make your loop grab and display them starting at the first one.
In your code
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {
you're fetching a row (with fetch_array), throwing it away and then fetching another row (with fetch_assoc). That's probably why you're only seeing one row instead of two

displaying mysql data in marquee

i want to make an news bar, by selecting mysql data and display it in and marquee, the problem that all the data are displayed at the same time in different lines, what i need is to display the data line by line.
code:
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
while ($row = mysql_fetch_assoc($news)) {
echo "<marquee style='float:bottom;'><font color='snow'>{$row['ann_title']}: {$row['ann_text']}</font></marquee>";
It's because you're creating a new marquee element every iteration of your while loop. Use something like the below code instead.
$news = mysql_query("SELECT ann_title, ann_text FROM o_postnews_conference");
echo "<marquee style='float:bottom;'><font color='snow'>";
while ($row = mysql_fetch_assoc($news))
echo "{$row['ann_title']}: {$row['ann_text']} ";
echo "</font></marquee>";
Although you should know that marquee has been deprecated and you should be using CSS3/javascript instead.

parse_str problems

I’m sorting a list and using Ajax to update a database. I need help parsing the string. This is the query string that I need to parse:
images_list[]=32&images_list[]=95&images_list[]=97&images_list[]=96&images_list[]=102&images_list[]=103&images_list[]=99&images_list[]=101&images_list[]=98&john=hi
I have put john=hi to test to see if the string is actually being sent via Ajax to the processor.php file. I am able to get the variable john=hi from the URL, so the query string is being sent perfectly fine. This is my code so far, but I can't seem to access the data I need, it appears as if nothing is there:
<?php
// Connect to the database
require_once('connect.php');
parse_str($_GET['images_list']);
for ($i = 0; $i < count($images_list); $i++) {
$id = $images_list[$i];
mysql_query("UPDATE images SET ranking = '$i' WHERE id = '$id'");
echo $images_list[$i];
}
?>
$_GET['images_list'] is an array of integers. There's nothing to parse there. PHP already did it for use. So, skip the parse_str part and easily use $_GET['images_list'] instead of $images_list.
Whole code:
<?php
//Connect to DB
require_once('connect.php');
foreach ($_GET['images_list'] as $i => $id) {
mysql_query("UPDATE images SET ranking = " .
mysql_real_escape_string($i) .
" WHERE id = " .
mysql_real_escape_string($id));
echo $id;
}
?>

Categories