如何正确连接此字符串?

时间:2014-01-10 10:53:23

标签: php

<?php 
    include 'connect.php' ; 

    session_start() ; //basically for all the session data we need 

    $user = $_SESSION['username'] ; 

    $queryFUF = mysql_query("SELECT friends FROM profile WHERE user = '$user'") ;        //query for retrieving the users friends 

    while($arrayFUF = mysql_fetch_array($queryFUF)) {

            $usersFriends = "".$arrayFUF['friends']."" ; //spliting friends out of the array we just got 
    } 

     $strWordCountFriends = str_word_count($usersFriends,1,'_0.9') ;

     foreach($strWordCountFriends as $key => $value) {

        $Ors = 'user = \''.$value.'\' OR ' ;

                echo $Ors;
     } 

echo '<br>';

$mysqlNewsQueryCounstructRaw = 'SELECT user,selfieid,selfieurl,caption,`currenttime`,likes FROM selfiestatusupdates WHERE '.$Ors  ; 

$mysqlNewsQueryCounstructEdited = substr($mysqlNewsQueryCounstructRaw,0,strlen($mysqlNewsQueryCounstructRaw) -3)  ;
echo $mysqlNewsQueryCounstructEdited.'<br>' ;

echo $mysqlNewsQueryCounstructRaw ;

我得到的输出非常令人困惑。这是输出

user = 'username' OR user = 'username1' OR 
SELECT user,selfieid,selfieurl,caption,`currenttime`,likes FROM selfiestatusupdates WHERE user = 'username' 
SELECT user,selfieid,selfieurl,caption,`currenttime`,likes FROM selfiestatusupdates WHERE user = 'username' OR 

当我回显 $ mysqlNewsQueryCounstructEdited

时,为什么不会显示变量 $ Ors 的第一部分?

1 个答案:

答案 0 :(得分:1)

要连接字符串,您需要使用.=

因此,$Ors = ...应为$Ors .= ...

while($arrayFUF = mysql_fetch_array($queryFUF)) {
    $usersFriends .= "".$arrayFUF['friends']."" ; //spliting friends out of the array we just got 
}

并且

foreach($strWordCountFriends as $key => $value) {
    $Ors .= 'user = \''.$value.'\' OR ' ;
    echo $Ors;
} 

请参阅PHP String Operators