php + mysql +如何添加逗号来拆分数据库中的字符串

时间:2013-04-20 12:20:08

标签: php mysql explode implode

我正在创建一个朋友请求页面,它需要将朋友请求作为数组,而不是用逗号分隔每个用户名。

我不知道如何使用内爆或爆炸我确实使用了两者但是没有人给我结果,因为我想要

当我按下accpet按钮时出现此错误

警告:implode():在第59行的C:\ wamp \ www \ Unnamed Site 2 \ resources \ friend_requests.php中传递的参数无效

谁能帮助我?

friend_request.php

<?php
session_start();

if($sessionlogin = isset($_SESSION['login']))
 {

   $login = ($_SESSION['login']);
   $userid = ($_SESSION['user_id']);
   $login_user = ($_SESSION['username']);
   $fname = ($_SESSION['first_name']);
   $lname = ($_SESSION['last_name']);
   $sessionaddres =($_SESSION['address']);



 // require_once('header.php'); 
 require_once('for members/scripts/connect.php');
  //find friend request
    $findrequests = mysql_query("SELECT * FROM friend_requests WHERE user_from = '$login_user'") or die(mysql_error());
$numrows = mysql_num_rows($findrequests);
   if($numrows == 0)
  {

      echo "YOU HAVE no friend Requests at this time";
      $user_from = "";

  }
    else
  {
       while($get_row =  mysql_fetch_assoc($findrequests))
     {
         $id = $get_row['id'];
         $user_to = $get_row['user_from'];
         $user_from = $get_row['user_to'];

            echo '' . $user_from .   '   wants to be  friend' . '<br />';


            //var_dump($user_from);
            //var_dump($user_to);
            //echo "your user_to is '$user_to'";
            //echo "your user_from is '$user_from'";




?>



<?php

 if(isset($_POST['acceptrequest'.$user_from]))
 {
     //select the friend array  row  from the logged in user
     $get_friend_check = mysql_query("SELECT friend_array FROM user WHERE user_name = '$login_user'") or die(mysql_error());
     $get_friend_row = mysql_fetch_assoc($get_friend_check);
     $friend_array = $get_friend_row['friend_array'];
     $friendArray_explode = implode(",", $friend_array);
     $friend_array_count = count($friendArray_explode);


     //select the friend array  row from the user  who sent the friend request 
     $get_friend_check_friend = mysql_query("SELECT friend_array FROM user WHERE user_name = '$user_from'") or die(mysql_error());
     $get_friend_row_friend = mysql_fetch_assoc($get_friend_check_friend);
     $friend_array_friend = $get_friend_row_friend['friend_array'];
     $friendArray_explode_friend = implode(",", $friend_array_friend);
     $friend_array_count_friend = count($friendArray_explode_friend);

     if($friend_array == "")
     {
       $friendArray_count = count(NULL);    

     }
        //var_dump($friendArray_count);

    if($friend_array_friend == "")
     {
       $friendArray_count_friend = count(NULL);  
     }

     if (@$friendArray_count == NULL)
     {
         $add_friend_query = mysql_query("UPDATE user SET friend_array = CONCAT(friend_array, '$user_from') WHERE user_name = '$login_user'");
     }

     if(@$friendArray_count_friend == NULL)
     {
         $add_friend_query = mysql_query("UPDATE user SET friend_array = CONCAT(friend_array, '$user_to') WHERE user_name = '$user_from'");
     }
     /*
     if(@$friendArray_count >= 1)
     {
         $add_friend_query = mysql_query("UPDATE user SET friend_array = CONCAT(friend_array, ',$user_from') WHERE user_name = '$login_user'");
     }
     /*
         if(@$friendArray_count_friend >= 1)
         {
         $add_friend_query = mysql_query("UPDATE user SET friend_array = CONCAT(friend_array, ',$user_to') WHERE user_name = '$user_from'");
     }

      */
     // for delete request  

     $delete_request = mysql_query("DELETE FROM friend_requests WHERE user_to = '$user_to' && user_from = '$user_from'") or die(mysql_error());

     echo "YOU are now friends!!";
    //header("Location: friend_requests.php");

 }


 if(isset($_POST['ignorerequest'.$user_from]))
 {
      $ignore_request = mysql_query("DELETE FROM friend_requests WHERE user_to = '$user_to' && user_from = '$user_from'")      or die(mysql_error());

     echo "Request ignored !!";
     header("Location: friend_requests.php");

 }

?><br />
<br />
<br />
<br />
<br />

<form action="friend_requests.php" method="post">
               <input type="submit" name="acceptrequest<?php echo $user_from; ?>" value="Accept">
               <input type="submit" name="ignorerequest<?php echo $user_from; ?>" value="Ignor">
</form>

<?php
   }
  }
 }

?>

1 个答案:

答案 0 :(得分:0)

试试这个

      $get_friend_check = mysql_query("SELECT friend_array FROM user WHERE user_name = '$login_user'") or die(mysql_error());

 $your_array = array();
 while ($get_friend_row = mysql_fetch_array($get_friend_check)) {
 $your_array[] = $get_friend_row['friend_array'];
 }

 $friendArray_explode = implode(",", $your_array);
 echo $friendArray_explode;
  • 你也可以直接在mysql中使用implode
像那样

$get_friend_check = mysql_query("SELECT group_concat(friend_array SEPARATOR ',')
                                 FROM user WHERE user_name = '$login_user'") 
or die(mysql_error());