0

I have created some Javascript that makes an AJAX POST request to the PHP file below, titled 'listgroupsall.php'. However, I am not getting the proper data back.

The AJAX request and the PHP script are supposed to return groups that a member of the site belong to and display some information about them on the page that can be clicked to go to that group's page.

Here is the PHP code:

<?php

//CONTAINS MYSQL SERVER CONNECTION INFO AND OTHER GOODIES
require_once $_SERVER['DOCUMENT_ROOT'] . '/filepath/config.php';

//VARIABLES PASSED IN FROM THE AJAX REQUEST
$uid = $_POST['uid'];
$tid = $_POST['tid'];
$p = $_POST['page'];
$filter = $_POST['fieldcontent'];

//FORM MYSQL QUERY
$gaq = "SELECT g.gid, g.groupname, g.dispname, g.avpath FROM groups AS g INNER JOIN users_groups AS ug ON g.gid = ug.gid AND ug.uid = " . mysql_real_escape_string($tid);

//ADD CONSTRAINT BASED ON UID IF IT IS NOT PASSED IN AS 0
if($uid!="0"){
$gaq .= " INNER JOIN users_groups AS ug ON g.gid = ug.gid AND ug.uid = " . mysql_real_escape_string($uid); }

//ADD CONSTRAINT BASED ON FILTER IF IT IS NOT AN EMPTY STRING
if($filter!=""){
$gaq .= " WHERE g.dispname LIKE '%" . mysql_real_escape_string($filter) . "%'"; }

//ORDER THE RESULTS BY ASCENDING g.dispname
$gaq .= " ORDER BY g.dispname ASC";
//GET AMOUNT OF PAGES REQUIRED TO DISPLAY ALL ITEMS RETURNED,
// SUCH THAT 10 ITEMS FIT ON 1 PAGE
$pnum = ceil(mysql_num_rows(mysql_query($gaq))/10);
//DISPLAY 10 RESULTS, STARTING ON 'PAGE' PASSED IN TO AJAX REQUEST
$gaq .= " LIMIT " . ($p*10)-10 . ",10";
//EXECUTE FULL MYSQL QUERY
$gaqr = mysql_query($gaq);

//APPEND A STRING $galisthtml WITH HTML OF THAT BLOCK
// WHICH CAN BE WRITTEN STRAIGHT TO THE PAGE
$galisthtml = "";
while($row = mysql_fetch_array($gaqr)){
  $gid = $row['gid'];
  $gname = $row['groupname'];
  $gdname = $row['dispname'];
  $gavpath = $row['avpath'];

  $galisthtml .= "<a href='http://galacticsoftware.net/prop/group.php?groupname=" . $gname . "'><div class=gblock><div class=gblockavcont>";
  $galisthtml .= "<img src='/prop/assets/img/avatars/group/" . $gavpath . "' Alt='Group Avatar' height=48px width=48px></div>";
  $galisthtml .= "<div class=gblocktextcont><h1>" . $gdname . "</h1>&" . $gname . "</div></div></a>";
}

//RETURN THE PAGE NUMBERS TOTAL
//AND THE GROUPS LISTING HTML
//(IN THAT ORDER)
echo json_encode(array($pnum, $galisthtml));

?>

When I call the AJAX request, I am first giving it the values of

uid = 0
tid = 23
p = 1
fieldcontent = ""

When the AJAX request returns, it sends back

[1,""]

The integer is correct, saying that all groups returned will fit on 1 page (there are 4 of them total). However, it appears that the string concatenation inside the while loop never executes. If I define $galisthtml with a non-empty string it is successfully returned, so I know I am returning my data correctly.

The while loop I have is used successfully in another area of my page when it echoes strings instead of concatenating a string, and after running the same MYSQL query.

I have researched this issue and even seen others concatenate strings inside a while loop like this with no issues. I have no idea what the issue could be.

  • 1
    You probably don't have any results – Jage Aug 21 '15 at 03:59
  • If I type the very same mysql query that this code would generate, I get 4 results, exactly as expected. However I will return mysql_num_rows instead of the concatenated string, just right now, solely for test purposes. – StrangeOrange Aug 21 '15 at 04:04
  • Learn how to use `xdebug` to debug your code and see what's going on. Then [stop using deprecated and insecure `mysql_query` and switch to PDO](http://stackoverflow.com/q/12859942/1233508). – DCoder Aug 21 '15 at 04:09
  • Good thinking, @Jage , it appears that after `$gaq .= " LIMIT " . ($p*10)-10 . ",10";` this causes `mysql_num_rows(mysql_query($gaq))` to return `null`. – StrangeOrange Aug 21 '15 at 04:11
  • Simply putting parenthesis around `($p*10)-10` in `$gaq .= " LIMIT " . ($p*10)-10 . ",10";` fixed it! Thanks Jage for pointing me in the right direction to find that. – StrangeOrange Aug 21 '15 at 04:15
  • @StrangeOrange , yw, glad you found that. – Jage Aug 21 '15 at 07:20

1 Answers1

1

You should close expression in parenthesis:

$gaq .= " LIMIT " . (($p*10)-10) . ",10"; //<- Note bracket around expression

To analyse such issues, you should always print generated SQL statement.

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30