虽然循环没有连接字符串

时间:2015-08-21 03:57:38

标签: php mysql while-loop concatenation

我创建了一些Javascript,它向下面的PHP文件发出一个AJAX POST请求,名为' listgroupsall.php'。但是,我没有收到适当的数据。

AJAX请求和PHP脚本应该返回该站点成员所属的组,并在页面上显示有关它们的一些信息,可以单击该组以转到该组的页面。

这是PHP代码:

<?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));

?>

当我调用AJAX请求时,我首先给它

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

当AJAX请求返回时,它会发回

[1,""]

整数是正确的,表示返回的所有组都适合1页(总共有4个)。但是,看起来while循环中的字符串连接永远不会执行。如果我用非空字符串定义$ galisthtml它会成功返回,所以我知道我正在正确地返回数据。

我在页面的另一个区域成功使用while循环,当它回显字符串而不是连接字符串时,并且在运行相同的MYSQL查询之后。

我已经研究过这个问题,甚至看到其他人在这样的while循环中连接字符串,没有任何问题。我不知道问题是什么。

1 个答案:

答案 0 :(得分:1)

您应该在括号中关闭表达式:

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

要分析此类问题,您应始终打印生成的SQL语句。