Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP&;while外while循环的输出_Php_Mysql_While Loop_Trim - Fatal编程技术网

PHP&;while外while循环的输出

PHP&;while外while循环的输出,php,mysql,while-loop,trim,Php,Mysql,While Loop,Trim,我的Php代码是 <?php include('session.php'); include('db.php'); $session=$_SESSION['agent']; $home1 = "SELECT fullname FROM users WHERE role='$session'"; $result1=mysql_query($home1) or die(mysql_error()); while ($row1=mysql_fetch_array($result1)) {

我的Php代码是

<?php
include('session.php');
include('db.php');

$session=$_SESSION['agent'];

$home1 = "SELECT fullname FROM users WHERE role='$session'";
$result1=mysql_query($home1) or die(mysql_error());
while ($row1=mysql_fetch_array($result1)) {

echo $gallery = "'".$row1[0]."',"; // output is 'sam','teja','multiplevaluessoon',


$home = "SELECT * FROM chat WHERE chat.from IN ('$gallery')";
}
$result=mysql_query($home) or die(mysql_error());

while ($row=mysql_fetch_array($result)) {

    $msg         =$row["message"];
    echo $randomUserId=$row["from"];
   echo $msg . "<br/>";
   }
 ?>
我得到的循环中的最后一个字符

 'sam''teja''multiplevaluessoon'
这是由于while循环而修剪所有逗号,我需要修剪最后一个逗号,以便在where子句中执行它

请向我推荐任何可以利用的想法

像这样更改您的

while ($row1=mysql_fetch_array($result1)) {

      $gallery .= "'".$row1[0]."',";
      $home = "SELECT * FROM chat WHERE chat.from IN ('$gallery')";
}
echo rtrim($gallery,',');

删除逗号,如下所示

$gallery=substr_replace($gallery ,'',0,1);

这就是我在那里解释的,当我修剪时,它删除了所有逗号,因为它在while循环中。您需要在while循环之外执行此操作。。这就是为什么我将变量名设置为
$yourfinaltext
,但在循环外部,我不会得到与循环内部相同的输出,在循环外部,它只会打印列的最后一个值
while ($row1=mysql_fetch_array($result1)) {

      $gallery .= "'".$row1[0]."',";
      $home = "SELECT * FROM chat WHERE chat.from IN ('$gallery')";
}
echo rtrim($gallery,',');
$gallery=substr_replace($gallery ,'',0,1);
$rest = substr("abcdef", 0, -1);  // returns "abcde"