Master
Master

Reputation: 11

Php using a variable in a string (concatenation)

why i can't use this code ? and could anyone tell me what is the correct code ?

$lfr_prfid = "profile='$log_id'";

$lfrsql = "SELECT * FROM friends WHERE user2='$log_id' AND accepted='1'";
$lfrquery = mysqli_query($db_conx, $lfrsql);

while ($lfrrow = mysqli_fetch_array($lfrquery, MYSQLI_ASSOC)) {

$lfr_id = $lfrrow["id"];
$lfr_user1 = $lfrrow["user1"];
$lfr_user2 = $lfrrow["user2"];

$lfr_prfid += " OR profile='".$lfr_user1."'";

}

the last line i wrote this ( += ) and the code doesn't work so how can i do this in another way ? so i can use this in a SELECT statement .

$psql = "SELECT * FROM posts WHERE ".$lfr_prfid." ORDER BY postdate DESC LIMIT 0,20";
$pquery = mysqli_query($db_conx, $psql);

Upvotes: 0

Views: 85

Answers (2)

sushant-hiray
sushant-hiray

Reputation: 1898

Change your code to:

$lfr_prfid .= " OR profile='".$lfr_user1."'";

Concatenation in PHP is done with ., not with += as you have written.

Hope this helps!

Upvotes: 2

m59
m59

Reputation: 43745

$lfr_prfid.=

Concatenate with . not with +. The + is concatenation in javascript.

so, in php: $myVar.= 'foo';

and in javascript: myVar+= 'foo';

Update based on your edit:

Please, DO NOT use that in a database query. Use prepared statements or your code is dangerous.

Upvotes: 4

Related Questions