我有这个从mysql数据库获取数据的代码。
<?php
$sql = mysql_query( "SELECT * FROM users WHERE name = $name" );
while ( $row = mysql_fetch_array( $sql ) ) {
echo $row[id] . '|';
echo $row[name] . '|';
echo $row[add] . ':';
}
OUTPUT : 12|jonathan|philippines:14|John|england:
?>
如何使用while?
删除:
的最后一个分隔符
答案 0 :(得分:2)
$sql = mysql_query( "SELECT `id`, `name`, `add` FROM users WHERE name = $name" );
$data = array();
while ( $row = mysql_fetch_array( $sql, MYSQL_NUM ) )
{
$data[] = implode('|', $row);
}
echo implode(':', $data);
如果您这样做会更加健壮,那么现在更改您的查询并不重要......它只会以您想要的格式输出您的字段
答案 1 :(得分:1)
$sql = mysql_query( "SELECT * FROM users WHERE name = $name" );
$counter = 0;
while ( $row = mysql_fetch_array( $sql ) ) {
if ($counter > 0) {
echo ":";
}
echo $row[id] . '|' . $row[name] . '|' . $row[add];
$counter++;
}
答案 2 :(得分:0)
<?php
$out = '';
$sql = mysql_query( "SELECT * FROM users WHERE name = $name" );
while ( $row = mysql_fetch_array( $sql ) ) {
$out.=$row[id] . '|';
$out.=$row[name] . '|';
$out.=$row[add] . ':';
}
echo substr($out,0,-1);
?>
答案 3 :(得分:0)
$sql = mysql_query( "SELECT * FROM users WHERE name = $name" );
$number = mysql_num_rows($sql);
$i = 1;
while ($row = mysql_fetch_assoc($sql)) {
echo $row[id] . '|';
echo $row[name] . '|';
echo $row[add] ;
if($i < $number) {
echo ':';
}
$i++;
}