-1

将 while 函数的结果放入字符串中的最佳做法是什么。

例如 :

while($row2 = mysql_fetch_assoc($select2)){
    echo "id:".$id."<br />";
    echo "Job Title: ".$title."<br />";
    echo "Email: ".$email."<br />";
}

$message = $row2;

这是我尝试过的,但找不到正确的方法。非常感谢

4

3 回答 3

2

连接它:

$message = '';

while($row2 = mysql_fetch_assoc($select2)){
    $message .= "id:".$id."<br />";
    $message .= "Job Title: ".$title."<br />";
    $message .= "Email: ".$email."<br />";
}

echo $message;
于 2012-09-10T13:53:57.667 回答
2

我更喜欢使用数组。

$message = "";
while($row2 = mysql_fetch_assoc($select2)){
   $message[] = "id:".$row2['id'];
   $message[] = "Job Title: ".$row2['title'];
   $message[] = "Email: ".$row2['email'];
}

echo implode('<br />', $message);
于 2012-09-10T13:56:58.390 回答
0

使用字符串 concat 运算符:

$message = "";
while($row2 = mysql_fetch_assoc($select2)){
   $message .= "id:".$id."<br />";
   $message .= "Job Title: ".$title."<br />";
   $message .= "Email: ".$email."<br />";
}

echo $message;
于 2012-09-10T13:54:50.600 回答