从表中显示多个ID列

时间:2014-02-09 16:12:27

标签: php mysql

目前我只显示第一个按升序排序的ID 我必须在同一个表中显示多个ID结果。我将如何完成这项任务?

<?php
   $dbhost ='localhost'; 
   $dbuser =‘user’; $dbpass =‘pass’;
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(!$conn){
       die('Could not connect: ' . mysql_error());
   }
   $sql = 'SELECT * FROM table_name ORDER BY id ASC LIMIT 0, 1';
   mysql_select_db('database_name’);
   $retval = mysql_query($sql, $conn);
   if(!$retval){
       die('Could not get data: ' . mysql_error());
   }
?>

<?php while($row = mysql_fetch_assoc($retval))
    {echo "{$row[‘full_name’]}”.”
    {$row[‘telephone']}"."
    {$row[‘email’]}”;}
    mysql_close($conn); 
?>

2 个答案:

答案 0 :(得分:1)

您所要做的就是将LIMIT 1更改为您想要的任何数字,或者只是删除它。删除它看起来像:

$sql = 'SELECT * FROM table_name ORDER BY id ASC'

答案 1 :(得分:0)

如果您希望显示所有ID,为什么还有限制?

您可以在脚本中使用while循环

例如:

while($row = mysql_fetch_assoc($test)){
                                      <div id="'.$row['id'].'">'.$row['id']'.</div>;
                                      }

将输出:

<div id="my id 1">my data 1</div>
<div id="my id 2">my data 2</div>
<div id="my id 3">my data 3</div>
.
. 
. 
and so on

您修改后的代码,用于输出不同div中的数据

<?php
$db = mysqli_connect("localhost", "user", "pass" ,"database name") or die("Could not connect database");//keep it in one line and use (mysqli) instead of (mysql) because newer mysql versions donot support (mysql)
$retreive=mysqli_query($db,'SELECT * FROM table_name ORDER BY id');//if youre connection is in another file you need to include the connection variable before the selection line.In this case $db is the connection variable
while($row=mysqli_fetch_array($retrieve))
{ the following code creates a div for each result
echo '<div class="'.$row['fullname'].'">'.$row['fullname'].'</div>';.
echo '<div class="'.$row['telephone'].'">'.$row['telephone'].'</div>';
echo '<div class="'.$row['email'].'">'.$row['email'].'</div>';
}
?>

在我的测试服务器上尝试了上面的代码,它可以正常工作